LED chaser program problem: all led lit up at the same time

I am trying to map out 10 leds controlling bits into an int, and use it to control an array of leds. I am trying to do a LED chaser, but all led lit up at the same time;

int led = 0;

void led_up () {
  bitWrite(PORTD, 0, bitRead(led, 0));
  bitWrite(PORTD, 1, bitRead(led, 1));
  bitWrite(PORTD, 2, bitRead(led, 2));
  bitWrite(PORTD, 3, bitRead(led, 3));
  bitWrite(PORTD, 4, bitRead(led, 4));
  bitWrite(PORTD, 5, bitRead(led, 5));
  bitWrite(PORTD, 6, bitRead(led, 6));
  bitWrite(PORTD, 7, bitRead(led, 7));
  bitWrite(PORTB, 0, bitRead(led, 8));
  bitWrite(PORTB, 5, bitRead(led, 9));
}

int main (void)
{
  //DDRD = 0b11111111; // set pin 0, 1,2,3, 4,5,6,7 as output
  // DDRB = 0b00100001; // set pin 8, 13 as output

  while (1) {

    for (int x = 0; x < 10; x++) {
      bitSet(led, x); // bitWrite(led, x, 1);   // led |= (1<<x);
      delayMicroseconds(1000);
      led_up();
    }

  }
}

Where do you ever clear a bit in led?

PaulS:
Where do you ever clear a bit in led?

int led =0; all led are lit at the same time ever after I pressed the reset button.

arduinomagbit:
int led =0; all led are lit at the same time ever after I pressed the reset button.

The value in led is set to 0 once. 1000 microseconds (that's 1 millisecond) later, the value is no longer 0. It is 0b0000000001. 1 millisecond later, it is 0b0000000011. After 10 milliseconds, the value is 0b1111111111. Once it gets to 0b1111111111, it NEVER gets to be any other value.

If you blink, you'll miss the 10 milliseconds it takes for the LEDs to light up individually.

If you don't believe me, change the delayMicroseconds() call to delay(1000).

PaulS:
The value in led is set to 0 once. 1000 microseconds (that’s 1 millisecond) later, the value is no longer 0. It is 0b0000000001. 1 millisecond later, it is 0b0000000011. After 10 milliseconds, the value is 0b1111111111. Once it gets to 0b1111111111, it NEVER gets to be any other value.

If you blink, you’ll miss the 10 milliseconds it takes for the LEDs to light up individually.

If you don’t believe me, change the delayMicroseconds() call to delay(1000).

you are right about the delayMicroseconds(), but still doesn’t work; now only the first led is lit; the rest and off and it doesn’t move at all.

int led = 0;

void led_up () {
  bitWrite(PORTD, 0, bitRead(led, 0));
  bitWrite(PORTD, 1, bitRead(led, 1));
  bitWrite(PORTD, 2, bitRead(led, 2));
  bitWrite(PORTD, 3, bitRead(led, 3));
  bitWrite(PORTD, 4, bitRead(led, 4));
  bitWrite(PORTD, 5, bitRead(led, 5));
  bitWrite(PORTD, 6, bitRead(led, 6));
  bitWrite(PORTD, 7, bitRead(led, 7));
  bitWrite(PORTB, 0, bitRead(led, 8));
  bitWrite(PORTB, 5, bitRead(led, 9));
}

int main (void)
{
  //DDRD = 0b11111111; // set pin 0, 1,2,3, 4,5,6,7 as output
  // DDRB = 0b00100001; // set pin 8, 13 as output

  while (1) {

    for (int x = 0; x < 10; x++) {
      bitWrite(led, x, 1); // bitWrite(led, x, 1);   // led |= (1<<x);
      led_up();
      delay(1000);
    }

  }
}

Aside from the change to the delay interval, you also changed how you are defining values for led. Stick to one method until you understand what that does.

In the for loop, Serial.print() the value in led to see what it actually looks like. Share that information.

PaulS:
Aside from the change to the delay interval, you also changed how you are defining values for led. Stick to one method until you understand what that does.

In the for loop, Serial.print() the value in led to see what it actually looks like. Share that information.

changed led to uint16_t; here is the print out. it print once every second as expected.

led:1
led:11
led:111
led:1111
led:11111
led:111111
led:1111111
led:11111111
led:111111111
led:1111111111

it print once every second as expected.

But only one LED lit up? Which one?

PaulS:
But only one LED lit up? Which one?

yes. digital pin 0, the first one; I don't get this.

arduinomagbit:
yes. digital pin 0, the first one; I don't get this.

I don't either, since the first pin you are trying to light up is the first pin on PORTD. If that is really digital pin 0, then using Serial to write data to the serial port is going to f**k with the rest of the code, since using Serial changes the mode of pins 0 and 1, which is going to cause your code to do weird things.

PaulS:
I don't either, since the first pin you are trying to light up is the first pin on PORTD. If that is really digital pin 0, then using Serial to write data to the serial port is going to f**k with the rest of the code, since using Serial changes the mode of pins 0 and 1, which is going to cause your code to do weird things.

you asked me to print out the values of led, so I did. The board that I am programming doesn't have serial to usb converter, so I used a nano to run the code with added serial code to display the values of led. either way, serial will still work perfectly fine with my code because PORTD only set the internal 10k resistor to power the LED. pin0 and all other pins are still input pins.

PaulS:
I don't either, since the first pin you are trying to light up is the first pin on PORTD. If that is really digital pin 0, then using Serial to write data to the serial port is going to f**k with the rest of the code, since using Serial changes the mode of pins 0 and 1, which is going to cause your code to do weird things.

You can still use pin 0 and pin 1 while doing serial, despite that you are taught not to use them in arduino. here is an example.

boolean test=0;

void setup()
{
  Serial.begin(9600);
  pinMode (0, OUTPUT);
  pinMode (1, OUTPUT);
}

void loop() {

    digitalWrite (0, test);
    digitalWrite (1, test);    
    Serial.println("hello:");
      _delay_ms(1000); 
      test=!test;

}
hello:
hello:
hello:
hello:
hello:
hello:
hello:
hello:
hello:
hello:
hello:
hello:
hello:
hello:
hello:
hello:
hello:
hello:
hello: