Using Bluetooth to increment a counter?

I'm obviously doing something wrong but I can't figure out what...
I have a simple Bluetooth app on my phone that has a button that sends the character ">" when it is pressed and then the character "." when it is let go. I'm trying to write an Arduino Sketch that has a counter that will continuously increment by 1 while the the button on the Android app is pressed (after the Arduino receives the character ">") and then stop when the Arduino receives the character "." (when the android app button is released).

This is my sketch but it's not working at all. It receives the characters from the phone alright and prints them in the Serial Monitor but it doesn't look like it's going into the second "if" loop to increment and display the counter.

I would really appreciate some help with this as I've been stuck here for quite some time.

long Counter = 0;              
char Incoming_value = 0;               

void setup() {
    Serial3.begin(9600);
    Serial.begin(9600);     
}

void loop() {

  if(Serial3.available() > 0)  {
     Incoming_value = Serial3.read();      //Read the incoming data and store it into variable Incoming_value
     Serial.println(Incoming_value);        //Print Value of Incoming_value in Serial monitor
  

  if (Incoming_value == ">") {
    Serial.println(Incoming_value);        //Print Value of Incoming_value in Serial monitor
    Counter = Counter + 1;
    Serial.println(Counter);        //Print Value of the counter in Serial monitor
  }
  }

}

Incoming_value is a char not a string

Try

if (Incoming_value == '>')

The way I would do this is with with boolean controls on the counter set by the serial input. Here is a demonstration of what I mean using input from the monitor. Adapting to to bluetooth will be simple. The printout of the counter is on a timer to keep things readable.

long counter = 0;
boolean runCounter = false;
char Incoming_value = 0;
unsigned long lastPrint;

void setup() {
  //Serial3.begin(9600);
  Serial.begin(9600);
  Serial.println("Run Counter Demo");
}

void loop() {

  if (Serial.available() > 0)
  {
    Incoming_value = Serial.read();
    if (Incoming_value == '>' || Incoming_value == '.')
      Serial.println(Incoming_value);
  }

  if (Incoming_value == '>' && runCounter == false)
  {
    Serial.println("Start Counter");
    runCounter = true;
  }

  if (Incoming_value == '.' && runCounter == true)
  {
    Serial.println("Stop Counter");
    runCounter = false;
    counter = 0;//reset counter value is required
  }

  if (runCounter && millis() - lastPrint >= 500)
  {
    lastPrint = millis();
    counter++;
    Serial.println(counter);
  }
}

UKHeliBob:
Incoming_value is a char not a string

Try

if (Incoming_value == '>')

Thank you, I tried it and it works much better! I guess I will need to read up on ’ vs ". I also had to change the second “if” to a “while” and add another Serial3.available() and Serial3.read() in there. Like I said it works now but I’m not sure if what I did is the correct way to do things.

This is my updated code:

long Counter = 0;              
char Incoming_value = 0;               

void setup() {
    Serial3.begin(9600);
    Serial.begin(9600);     
}

void loop() {

  if(Serial3.available() > 0)  {
     Incoming_value = Serial3.read();       //Read the incoming data and store it into variable Incoming_value
     Serial.println(Incoming_value);        //Print Value of Incoming_value in Serial monitor
  

  while (Incoming_value == '>') {
//    Serial.println(Incoming_value);       //Print Value of Incoming_value in Serial monitor
    Counter = Counter + 1;
    Serial.println(Counter);                //Print Value of Incoming_value in Serial monitor
      if(Serial3.available() > 0){
      Incoming_value = Serial3.read(); 
  }
  }
  while (Incoming_value == '<') {
//    Serial.println(Incoming_value);       //Print Value of Incoming_value in Serial monitor
    Counter = Counter - 1;
    Serial.println(Counter);                //Print Value of Incoming_value in Serial monitor
      if(Serial3.available() > 0){
      Incoming_value = Serial3.read(); 
  }
  }
}
}

cattledog:
The way I would do this is with with boolean controls on the counter set by the serial input. Here is a demonstration of what I mean using input from the monitor. Adapting to to bluetooth will be simple. The printout of the counter is on a timer to keep things readable.

long counter = 0;

boolean runCounter = false;
char Incoming_value = 0;
unsigned long lastPrint;

void setup() {
 //Serial3.begin(9600);
 Serial.begin(9600);
 Serial.println("Run Counter Demo");
}

void loop() {

if (Serial.available() > 0)
 {
   Incoming_value = Serial.read();
   if (Incoming_value == '>' || Incoming_value == '.')
     Serial.println(Incoming_value);
 }

if (Incoming_value == '>' && runCounter == false)
 {
   Serial.println("Start Counter");
   runCounter = true;
 }

if (Incoming_value == '.' && runCounter == true)
 {
   Serial.println("Stop Counter");
   runCounter = false;
   counter = 0;//reset counter value is required
 }

if (runCounter && millis() - lastPrint >= 500)
 {
   lastPrint = millis();
   counter++;
   Serial.println(counter);
 }
}

Thank you! I will experiment with this. :slight_smile: