/4_arduino_counter_EN.ino Secret
Created
February 7, 2018 22:21
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define BUTTON 8 | |
int counter = 0; //We store the number 0 in an integer type variable named counter | |
String msg; //We declare the variable msg of type String (characheteres chain) | |
boolean sign = true; //We declare a boolean variable (true or false) named sign that will determine whether to add or remove 1 | |
//Other common types of variables are float (decimal number) and char (characteres) | |
void setup() { | |
// put your setup code here, to run once: | |
pinMode(BUTTON, INPUT); // Set BUTTON as an INPUT | |
Serial.begin(9600); //Initializes serial communication at 9600 baud | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
if (Serial.available() ) | |
{ | |
//If a message has been received do this | |
msg = Serial.readString(); //Stores the received message in the msg variable | |
Serial.print("--> "); | |
Serial.println(msg); | |
if(msg == "+")//If the message is equal to + | |
{ | |
sign = true; //Then we set sign to true. | |
} | |
else if (msg == "-") //Otherwise if the message is equal to + | |
{ | |
sign = false; | |
} | |
else //If not (if not a + or -) do | |
{ | |
Serial.println("Type + or -"); | |
} | |
} | |
if(digitalRead(BUTTON) == HIGH) //If the input is at 5V = button pressed then do | |
{ | |
while(digitalRead(BUTTON) == HIGH) //As long as the condition (button pressed) is true do: | |
{ //This loop is used to wait until the button is released to avoid counting a number of times | |
delay(100); //Wait 100ms | |
} | |
if(sign == true) // If sign = true (if we sent a +) | |
{ | |
counter++; //Add 1 (it's like doing: counter = counter +1;) | |
} | |
if(sign == false) // If sign = false (if we sent a -) | |
{ | |
counter--; //Remove 1 (it's like doing: counter = counter -1;) | |
} | |
Serial.println(counter); //Display the counter | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment