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 pins to input so we can read the values. Now we need to add the code to the loop() function that will read the sensors. Let's start...