O'Reilly logo

Mastering Arduino by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required