Now I will tell you how to use "Switch and Case" in Arduino. You can use it instead of "if". Here you can see a code: int i = Random(5);//generate Random number from 0 to 4 switch(i) { case 0: //Action if value is 0 break; case 1: //Action if value is 1 break; case 2: //Action if value is 2 break; case 3: //Action if value is 3 break; case 4: //Action if value is 4 break; }
When I was searching the WEB, I have found very easy way how to control buttons in Arduino.
Connections:
OK, and now code:
int ledPin = 13;// choose the pin for the LED int inPin = 7;// choose the input pin (for a pushbutton) int val = 0;// variable for reading the pin status void setup() { pinMode(ledPin, OUTPUT);// declare LED as outputpinMode(inPin, INPUT);// declare pushbutton as input } void loop() { val = digitalRead(inPin);// read input value if (val == HIGH)//check if the input is HIGH (button released) { digitalWrite(ledPin, LOW);// turn LED OFF } else { digitalWrite(ledPin, HIGH);// turn LED ON } }
It (array) will be useful for you if you don't want to initialise tones of variables. For example: instead of using two int variables we can create one int array, which will store two int values. //instead of using it: int a = 2; int b = 4; //we can use it: int array[2] = {2, 4}; Syntax: typeOfVariable anyNameYouWant[emountOfValuesToStore] = {vars};