Similar to a button, a tilt-switch is a type of input that can tell our board when a certain condition has occurred. Unlike a button, however, a tilt switch is much more likely to be found on the bot itself rather than on a controller.
Just like the previous chapter, download the material from the Arduino starer kit tutorial here, follow the steps, and use the code below.
Code:
/*****************************************/
const int ledPin = 13;//the led attach to
void setup()
{
pinMode(ledPin,OUTPUT);//initialize the ledPin as an output
pinMode(2,INPUT);
digitalWrite(2, HIGH);
}
/******************************************/
void loop()
{
int digitalVal = digitalRead(2);
if(HIGH == digitalVal)
{
digitalWrite(ledPin,LOW);//turn the led off
}
else
{
digitalWrite(ledPin,HIGH);//turn the led on
}
}
/**********************************************/
Got this one down? Reach out for help if you need it. Don’t forget to really read the code that is being provided and try to understand how it works, ask your coach or mentors questions as you go along.
We are covering a lot and glazing over many details here. The goal is to balance on a fine line between throwing too much info at you vs laying a workable foundation of coding knowledge. You aren’t going to learn everything you need to be a programmer with just these tutorials alone, but you will learn enough to be dangerous and branch out from there.
When you are ready head to the next section where we will cover a couple chapters of the starter kit’s tutorial on buzzers.