Code

We will begin the code with three macros that define the pins that the three sensors are connected to. The macros will look like this:

#define COLLISION_SWITCH 4
#define IR_SENSOR 3
#define RANGE_SENSOR A1

These macros show that the crash sensor is connected to digital pin 4, the infrared sensor is connected to digital pin 3 and the ultrasonic rangefinder is connected to analog pin 1. Now we need to set the mode for the two digital pins that we are using and also initiate the serial monitor. We can do this by adding the following code to the setup() function:

Serial.begin(9600);
pinMode(COLLISION_SWITCH, INPUT);
pinMode(IR_SENSOR, INPUT); 

This starts off by initiating the serial monitor and then configures the crash and infrared sensor ...

Get Mastering Arduino now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.