1

EDITED: I got the program to complete the loop, now it's just that it gives me a bunch of 0's before it completes the loop - so here's an updated version of my code.

Trying to create a program that first turns LEDs on one after one, and once they're all lit up - turns them off one after one, and keeps turning them on and off. I have succeeded in lighting them up and turning them off, but the program seems to get stuck for a bit when it's turned them all off before it completes the loop and starts over.

int t=1000;
unsigned long time;
int pin;
int value;
int a;
int b;

void setup() {
  // put your setup code here, to run once:
  for(pin=2; pin<8; pin++){
    pinMode(pin, OUTPUT);
  }

  Serial.begin(9600);

}

void more(){
  for(int i=1; i<8; i++){
    for(pin=i; pin<8; pin++){
      digitalWrite(pin,HIGH);
      a=bitRead(PORTD,7);
      b=bitRead(PORTD,2);

      for(pin=2; pin<8; pin++){
        value=bitRead(PORTD,pin);
        if(value==1){
          Serial.print("1   ");
        }
        else{
          Serial.print("0   ");
        }
      }
      Serial.println();
      Serial.println();
      delay(t);
    }
  }
}
void less(){
  for(int j=7; j>=2; j--){
    for(int p=j; p>1; p--){
      digitalWrite(p,LOW);
      a=bitRead(PORTD,7);
      b=bitRead(PORTD,2);

      for(pin=2; pin<8; pin++){
        value=bitRead(PORTD,pin);
        if(value==1){
          Serial.print("1   ");
        }
        else{
          Serial.print("0   ");
        }
      }
      Serial.println();
      Serial.println();
      delay(t);
    }
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.print("Time: ");
  time=millis();
  Serial.println(time);

  a=bitRead(PORTD,7);
  b=bitRead(PORTD,2);

  do{
    more();
  }while(a==0);

  do{
    less();
  }while(b==1);
}
enirt
  • 11
  • 3
  • I'm not sure why you're reading the pins rather than let the code store their status. I'm also not sure you can read and write to a pin which I assume is set to output mode. – snoopen Feb 27 '16 at 13:03
  • Actually you're edit 2 makes perfect sense. http://stackoverflow.com/questions/6160963/how-can-i-digitalread-a-pin-that-is-in-pinmode-output – snoopen Feb 27 '16 at 13:07
  • okay, I'm new to all this so thanks for the tip! @snoopen – enirt Feb 27 '16 at 15:22
  • okay I'm going to mess around with it a bit and see what happens now – enirt Feb 27 '16 at 16:33
  • Did the anwer below sort out your original question? If so then accept that answer and repost your changed code as a new question. – Blurry Sterk Feb 29 '16 at 05:45

1 Answers1

1

a=0 is always false and b=1 is always true because they do assignment and evaluated as what is assigned.

Use == operator to compare numbers.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70