a simple increment count doesn't work

hi there, after adding a simple count++ it work as long as i don’t add the last line to reset it
else count = 0;

and so if i delete it, and it will not even print every time the A only after 150 times from starting from the pressing but it will sum the count and obviously the second if will never be true.

the code:

const int ledR = 2;
int count = 0;
char incomingByte = 0;						// a variable to read incoming serial data into

void setup() {
  Serial.begin(57600);                      // initialize serial communication
  pinMode(ledR, OUTPUT);
}

void loop() {

	if (Serial.available() > 0) {			// see if there's incoming serial data:
	incomingByte = Serial.read();           // read the oldest byte in the serial buffer

     if ( incomingByte == 'R' ) {
         digitalWrite(ledR, HIGH);
		 count++;
			if ((count >= 150) && (count < 300)) {
				Serial.print("A");
				count = 0;
			}
			else if (count >= 500) { 
				Serial.print("B");
				count = 0;
			}
//			else count = 0;				// need to comment it to work
      }
	  
//	Serial.println(count);	debug

  }											// end of if Serial.available

}											// end of loop function

it work as long as i don't add the last line to reset it
else count = 0;

Look at your code. Play computer. What happens when count is 0 and a character arrives? What is count after that happens? If you said anything other than 0, start over.

Oh yes, i got your point :slight_smile:
But if i don't reset it, the count number will remain in memory and maybe can arrive till 149, and after 5 minutes when i except to receive the print of A after 150, instead it will print it instanlty cause it was already too near to the 150, sorry for my bad english hope you got what i wanted to say.

hope you got what i wanted to say.

Unfortunately, no.

jonnys90:
Oh yes, i got your point :slight_smile:
But if i don’t reset it, the count number will remain in memory and maybe can arrive till 149, and after 5 minutes when i except to receive the print of A after 150, instead it will print it instanlty cause it was already too near to the 150, sorry for my bad english hope you got what i wanted to say.

If you properly format your code you can see your issue:

const int ledR = 2;
int count = 0;
char incomingByte = 0;

void setup() 
{
  Serial.begin(57600);// initialize serial communication
  pinMode(ledR, OUTPUT);
}

void loop() 
{
  if (Serial.available() > 0) 
  {
    incomingByte = Serial.read();
    if ( incomingByte == 'R' ) 
    {
      digitalWrite(ledR, HIGH);
      count++;
      if ((count >= 150) && (count < 300)) 
      {
        Serial.print("A");
        count = 0;
      }
      else if (count >= 500) 
      {
        Serial.print("B");
        count = 0;
      }
      //else count = 0;  // this condition is an extension of the: if ((count >= 150) && (count < 300)) 
    }
    // Serial.println(count); debug
  }
}

what happens?

if ((count >= 150) && (count < 300)) // if count is between 150 and 299  inclusive (which won't happen):
{
  Serial.print("A");
  count = 0;                         // reset it to zero
}
else if (count >= 500)               // if count is greater than 499 (which won't happen)
{
  Serial.print("B");
  count = 0;                         // reset it to zero
}
else count = 0;                      // reset it to zero, which will happen every time neither of the above are true!!

Your code does not make any logical sense, try explaining exactly what you want to happen.

Even if you comment out that last "else" then you will only ever get 'A' to print every 150 incoming 'R's from serial.

PaulS:
Unfortunately, no.

Ps991:
try explaining exactly what you want to happen.

Even if you comment out that last "else" then you will only ever get 'A' to print every 150 incoming 'R's from serial.

Yes, you're right, i corrected it.
What i wanted to do is to try to turn on a led while is receiving a char in Serial.read()

Here is it:

if((currentMillis - previousMillis1) > 160) {
 countLed1 = 0;
 }
 if (ledisOn == false) {
 digitalWrite(ledPin, HIGH);
 previousMillis1 = currentMillis
 count++;
 ledisOn = true;
 if (count >= 150) { // button held 
 digitalWrite(ledPin, HIGH);
 Serial.println("led on");
 count = 0;
 }
 } else {
 digitalWrite(ledPin, LOW);
 ledisOn = false;
 }

The problem was that i needed to reset the count every time it's not has been receiving the char continuesly, so what i've done was to use Millis() to reset the count after a few ms that the char hasn't being received :slight_smile: Is this the right option? or there's something better that i can use?

You need to give us the entire code, I can't tell exactly what is happening based on a snippet of code.

What I can tell from what you have is that you are toggling the ledPin as fast as possible.

if (count >= 150) { // button held 
digitalWrite(ledPin, HIGH);
...

This code tells me nothing about a button, it just tells me that every 150 toggles, you then turn the LED on again, even though it is already on, so that is pointless...

Ps991:
What I can tell from what you have is that you are toggling the ledPin as fast as possible.

This code tells me nothing about a button, it just tells me that every 150 toggles, you then turn the LED on again, even though it is already on, so that is pointless...

In this slave arduino there aren't any button, it just receive over serial the char continuously in a few ms, so the count check if it has been reached otherwise it reset the count, it work fine, but i'm sure (maybe) that there are better option, maybe more compact and optimized, or not?

This is the last version updated

const int ledPin = 2;
int count = 0;
char incomingByte = 0; // a variable to read incoming serial data into
const long interval = 10;

unsigned long previousMillis1, previousMillis2;
boolean ledisOn = false;

void setup() {
  Serial.begin(57600);                      // initialize serial communication
  pinMode(ledPin, OUTPUT);
}

void loop() {

 if((currentMillis - previousMillis1) > interval && ledisOn) { 
 ledisOn = false; 
 digitalWrite(ledPin, LOW);
 }

 if((currentMillis - previousMillis1) > 160) {
 count = 0;
 }
 
 if (Serial.available() > 0) { // see if there's incoming serial data:
 incomingByte = Serial.read();         // read the oldest byte in the serial buffer
 
 if((currentMillis - previousMillis2) > 2000) {
 if ( incomingByte == 'C' ) { // while receiving continuously this char
 if (ledisOn == false) {
 digitalWrite(ledPin, HIGH);
 previousMillis1 = currentMillis;
 count++;
 ledisOn = true;
 if (count >= 150) { 
 digitalWrite(ledPin, HIGH); // turn on and stay on
 Serial.println("led on");
 previousMillis2 = currentMillis;
 count = 0;
 }
 } else {
 ledisOn = false;
 }
 }
 }

 }
  } // end of if Serial.available

} // end of loop function

jonnys90:
it work fine,[...]

but i'm sure (maybe) that there are better option, maybe more compact and optimized, or not?

you code doesn't compile.

BulldogLowell:
you code doesn't compile.

yeah sorry because i have removed 2 things that have nothing to do with it and maybe delete something wrongly, anyway here is the fixed version that i've just tried now and work at 100%:

const int ledPin = 11;
int count = 0;
char incomingByte = 0; // a variable to read incoming serial data into
const long interval = 10;

unsigned long previousMillis1, previousMillis2;
boolean ledisOn = false, ledisOnStay = false;

void setup() {
  Serial.begin(57600);                      // initialize serial communication
  pinMode(ledPin, OUTPUT);
}

void loop() {

  unsigned long currentMillis = millis(); // Grab snapshot of current time, this keeps all timing

  if((currentMillis - previousMillis1) > interval && ledisOn && !ledisOnStay) {
 ledisOn = false; 
 digitalWrite(ledPin, LOW);
 }

 if((currentMillis - previousMillis1) > 160) {
 count = 0;
 }
 
 if (Serial.available() > 0) { // see if there's incoming serial data:
 incomingByte = Serial.read();         // read the oldest byte in the serial buffer
 
 if((currentMillis - previousMillis2) > 2000) {
 if ( incomingByte == 'C' ) { // while receiving continuously this char
 if (ledisOn == false) {
 digitalWrite(ledPin, HIGH);
 previousMillis1 = currentMillis;
 count++;
 ledisOn = true;
 if (count >= 150) { 
 digitalWrite(ledPin, HIGH); // turn on and stay on
 Serial.println("led on");
 ledisOnStay = true;
 previousMillis2 = currentMillis;
 count = 0;
 }
 } else {
 ledisOn = false;
 ledisOnStay = false;
 }
 }
 }

 } // end of if Serial.available
Serial.println(count);
} // end of loop function

jonnys90:
yeah sorry because i have removed 2 things that have nothing to do with it and maybe delete something wrongly, anyway here is the fixed version that i've just tried now and work at 100%:

...

Nope! If you knew what the problem was, you wouldn't have asked the question, right? If you wish to remove parts of the code, re-write and compile it - then run it. Make sure the problem still exists in your redacted version.

ChrisTenone:
Nope! If you knew what the problem was, you wouldn't have asked the question, right? If you wish to remove parts of the code, re-write and compile it - then run it. Make sure the problem still exists in your redacted version.

I think you need to read the post before instead of commenting just the last reply..
In my Reply #6 i've told that i correct it and find A WORKING WAY for what i need BUT i put the code without some parts that have nothing to do with the part i had problems, and there i know and i apologize cause i have WRONGLY post the code without to make sure it can at least compile.

In my reply #10 instead i've attached the WORKING PERFECTLY 100% version that i've checked before to post it, AND i have just asked if there are BETTER OPTION maybe more optimized to do the same job, a part this, but NOW IT'S NOT MORE A PROBLEM, i'm pretty happy right now with the current version (Reply #10).

The question is, why does it need to be received continuously?
Why use serial when all you want is on or off, wouldn't a single wire do what you want much easier and simpler?
It is also weird that you turn on the LED, wait 10 ms before turning it off, then immediately turn it back on if another 'C' is read...
This whole setup is convoluted. Why did you choose this setup?

Ps991:
The question is, why does it need to be received continuously?
Why use serial when all you want is on or off, wouldn't a single wire do what you want much easier and simpler?

Because i will use 2 arduino connected each one to an Xbee module that use TX-RX, so this is why i use serial, and i will have to turn on the led only when i'm receiving the char.

Ps991:
It is also weird that you turn on the LED, wait 10 ms before turning it off, then immediately turn it back on if another 'C' is read...
This whole setup is convoluted. Why did you choose this setup?

In this way it can be turned on only when i send the char from the first arduino.
I also added the code to turn off it after 10 ms to allow it to turn on at full light before to turn off, and without the code to turn off, one time turned on it stay on..

But, wouldn't it be easier just to send a single character that tells it to turn on or off, rather than a continuous sending. Like, send 'A' to turn it on, 'B' to turn it off. Seems easier.

Yeah, but i need to turn on it just when i press the button on the master arduino, otherwise yes, it could be much more easier.

but i need to turn on it just when i press the button on the master arduino

So, look at the state change detection example. Send ONE character when the switch becomes pressed. Send a different character when the switch becomes released.

PaulS:
So, look at the state change detection example. Send ONE character when the switch becomes pressed. Send a different character when the switch becomes released.

Yeah, this is also a good alternative, thanks Paul!