Skip to content

Instantly share code, notes, and snippets.

@101Robotics
Created February 7, 2018 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 101Robotics/05f214a85f0e71994020129ce90d55d9 to your computer and use it in GitHub Desktop.
Save 101Robotics/05f214a85f0e71994020129ce90d55d9 to your computer and use it in GitHub Desktop.
#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