MySQL for the Internet of Things

(Steven Felgate) #1
ChApTEr 2 ■ hArdwArE for IoT SoluTIonS

Listing 2-1. Simple Sensor Sketch


/*
Simple Sensor - MySQL for the IOT


For this sketch, we explore a simple sensor (a pushbutton) and a simple
response to sensor input (a LED). When the sensor is activated (the
button is pushed), the LED is illuminated.
*/


int led = 13; // LED on pin 13
int button = 2; // button on pin 2


// the setup routine runs once when you press reset:
void setup() {
// initialize pin 13 as an output.
pinMode(led, OUTPUT);
pinMode(button, INPUT);
}


// the loop routine runs over and over again forever:
void loop() {
// read the state of the sensor
int state = digitalRead(button);


// if sensor engaged (button is pressed), turn on LED
if (state == HIGH) {
digitalWrite(led, HIGH);
}
// else turn off LED
else {
digitalWrite(led, LOW);
}
}


When you’ve entered the sketch as written, you’re ready to compile and run it.

Compiling and Uploading


Once you have the sketch written, test the compilation using the Compile button in the upper-left corner of
the IDE. Fix any compilation errors that appear in the message window. Typical errors include misspellings
or case changes (the compiler is case sensitive) for variables or methods.
After you’ve fixed any compilation errors, click the Upload button. The IDE compiles the sketch and
uploads the compiled sketch to the Arduino board. You can track the progress via the progress bar at the
lower right, above the message window. When the compiled sketch is uploaded, the progress bar disappears.


Testing the Project


Once the upload is complete, what do you see on your Arduino? If you’ve done everything right, the
answer is nothing. It’s just staring back at you with that one dark LED—almost mockingly. Now, press the
pushbutton. Did the LED illuminate? If so, congratulations: you’re an Arduino programmer!

Free download pdf