Counter issues

Status
Not open for further replies.
O

orvtech

Guest
Friends,

I apologize for this noobish question, I have over 15 years without doing any C programing what so ever.

I am trying to get a counter going in this script but it seems that it doesn't increment past 1. Any ideas what might be going on?
Code:
const int ledPin = 13; // choose the pin for the LED
int counter = 0;
char pin[4];

void setup() {
  pinMode(ledPin, OUTPUT); // declare LED as output
  Serial.begin(9600);
  while (!Serial) {}
}

void working(){
  if (counter <= 9999){
  digitalWrite(ledPin, LOW);
  delay(5000);
  digitalWrite(ledPin, HIGH);
  sprintf(pin, "%04d", counter);
  Serial.println(pin);
  }
  if (counter > 9999){
    digitalWrite(ledPin, LOW);
    delay(1000);
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
    delay(1000);
    digitalWrite(ledPin, HIGH);    
    delay(10000);
  }
  ++counter;
  //Keyboard.print(pin);
  //Keyboard.print(counter);
}

void loop(){
    working();
}
 
Last edited:
Solved my changing a bit my code as you will be able to see below I am formatting 'fakecounter' instead of counter:

Code:
const int ledPin = 13; // choose the pin for the LED
int counter = 0;
int fakecounter = counter;
char pin[4];

void setup() {
  pinMode(ledPin, OUTPUT); // declare LED as output
  Serial.begin(9600);
  while (!Serial) {}
}


void loop(){
  if (counter <= 9999){
  digitalWrite(ledPin, LOW);
  delay(4000);
  digitalWrite(ledPin, HIGH);
  delay(1000);
  sprintf(pin, "%04d", fakecounter);
  Serial.println(pin);
  }
  if (counter > 9999){
    digitalWrite(ledPin, LOW);
    delay(200);
    digitalWrite(ledPin, HIGH);
    delay(200);
    digitalWrite(ledPin, LOW);
    delay(200);
    digitalWrite(ledPin, HIGH);
    delay(200);
    digitalWrite(ledPin, LOW);
    delay(200);
    digitalWrite(ledPin, HIGH);
    delay(200);
    digitalWrite(ledPin, LOW);
    delay(800);
    digitalWrite(ledPin, HIGH);    
    delay(1000);
  }
  ++counter;
  fakecounter = counter;
  //Keyboard.print(pin);
  //Keyboard.print(counter);
}
 
This looks like a buffer overflow. You need at least 5 bytes in the array, because sprintf writes 4 bytes of text plus a zero at the end (in C, strings have an extra zero at the end, to you can tell you're at the end of the string when printing or using it in other ways).

Buffer overflows cause notoriously difficult to predict problems. On Arduino Uno, that extra byte may be overwriting something unimportant, but on Teensy 3.0 is may be clobbering something critical. Even on Uno, as you change your program, something important may be assigned to the memory where sprintf() writes the extra zero.

The fix is easy. Just change the size of the array to allow for the extra zero at the end.
 
Last edited:
Why is that zero not being displayed through Serial.println(pin); ?

PD: Thanks Paul for your help (almost 24/7). I al ready have my project up and running. Will post latter the results, code and photos.
 
Last edited:
Why is that zero not being displayed through Serial.println(pin); ?

.

The zero marks the end of the string; it's not printed. Another way is to declare pin as
Char pin[]="xxxx";

This will reserve 5 bytes for it.
 
Status
Not open for further replies.
Back
Top