Function blinkByte, is my system bewitched?

Hi
I must have been drunk or my system is bewitched. :slight_smile:
I want to write a function blinkByte() that blinks a byte on the built-in LED:

Currently it prints every step and I can't believe my eyes.

The function is called two times

blinkByte( 222, STDLED);
blinkByte( 043, STDLED);

The first time it splits the byte value correcly into 3 digits
Blinking value: 222
2 : 2 : 2
but it blinks 2 : 3 : 3 allthough i am using exactly the same for loop.

More strange: the second call shows
Blinking value: 35 (instead of 43) ? ? ?
0 : 3 : 5
and blinks 0: 4: 5.

I haven't got any hair lef on my head to rip of, and can't see where the mistake is.
Can someone open my eyes?

Here is the code:

#define SERIAL_SPEED 9600
#define STDLED     D4   // GPIO02 (& Console2 TX)

void blinkByte(byte value, byte pin)
{
  byte part;
  byte i;
  Serial.println("Mark ");   
  pinMode(pin, OUTPUT);
  digitalWrite(pin, false);
  delay(2000);
  digitalWrite(pin, true);
  Serial.print("Blinking value: ");  Serial.println(value);
  part = value / 100 % 10;
  Serial.print(part); Serial.print(" : ");
  for (i = 0; i <= part; i++) {
    digitalWrite(pin, false);
    delay(100);
    digitalWrite(pin, true);
    delay(500);
  }
  delay (1000);
  part = value / 10 % 10;
  Serial.print(part); Serial.print(" : ");
  for (i = 0; i <= part; i++) {
    digitalWrite(pin, false);
    delay(100);
    digitalWrite(pin, true);
    delay(500);    
  }
  delay(1000);
  part = value % 10;
  Serial.println(part);
  for (i = 0; i <= part; i++) {
    digitalWrite(pin, false);
    delay(100);
    digitalWrite(pin, true);
    delay(500);
  }
  delay(2000);
}

void setup() {
  // put your setup code here, to run once:
  // Serial initialisation
  Serial.begin (SERIAL_SPEED); // On USB port
  Serial.println();  
  blinkByte( 222, STDLED);
  blinkByte( 043, STDLED);
}

void loop() {
  // put your main code here, to run repeatedly:
}

The serial monitor output is:

Mark 
Blinking value: 222
2 : 2 : 2
Mark 
Blinking value: 35
0 : 3 : 5

blinkByte( 043, STDLED); Why octal?

TheMemberFormerlyKnownAsAWOL:
blinkByte( 043, STDLED); Why octal?

OK I found that out too.
One problem less...
The main problem remains: why does it blink wrong for the 2nd and 3rd digit?

Something to do with "<="?

Your programming is bewitched.

  1. Use HIGH and LOW instead of true and false. In your code replace false with HIGH and true with LOW
  2. Your for loops are incorrect:
  for (i = 0; i <= part; i++) {

should be:

  for (i = 0; i < part; i++) {

Fix those two things and your code will work.

ToddL1962:

  1. Use HIGH and LOW instead of true and false. In your code replace false with HIGH and true with LOW
  2. Your for loops are incorrect:
  for (i = 0; i <= part; i++) {

should be:

  for (i = 0; i < part; i++) {

I forgot to mention that I am compiling for an ESP8266, the LED in on with LOW.
I modified the loop accordingly, but now the first digit is wrong.
it blinks 1:2:2 instead of 2:2:2.

I can't understand why the loop behaves differently the first time.

Now i have found it.
I need a delay after the "mark" step.
The first blink merged with the mark.
Now it is correct:

#define SERIAL_SPEED 9600
#define STDLED     D4   // GPIO02 (& Console2 TX)

void blinkByte(byte value, byte pin)
{
  byte part;
  byte i;
  Serial.println("Mark ");   
  pinMode(pin, OUTPUT);
  digitalWrite(pin, LOW);
  delay(2000);
  digitalWrite(pin, HIGH);
  delay (1000); 
  Serial.print("Blinking value: ");  Serial.println(value);
  
  part = value / 100 % 10;
  Serial.print(part); Serial.print(" : ");
  for (i = 0; i < part; i++) {
    digitalWrite(pin, LOW);
    delay(100);
    digitalWrite(pin, HIGH);
    delay(500);
  }
  delay (1000);

  part = value / 10 % 10;
  Serial.print(part); Serial.print(" : ");
  for (i = 0; i < part; i++) {
    digitalWrite(pin, LOW);
    delay(100);
    digitalWrite(pin, HIGH);
    delay(500);    
  }
  delay(1000);

  part = value % 10;
  Serial.println(part);
  for (i = 0; i < part; i++) {
    digitalWrite(pin, LOW);
    delay(100);
    digitalWrite(pin, HIGH);
    delay(500);
  }
  delay(2000);
}

void setup() {
  // put your setup code here, to run once:
  // Serial initialisation
  Serial.begin (SERIAL_SPEED); // On USB port
  Serial.println();  
  blinkByte( 222, STDLED);
  blinkByte( 43, STDLED);
}

void loop() {
  // put your main code here, to run repeatedly:
}