Reputation: 11
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);
}
Upvotes: 1
Views: 166
Reputation: 75062
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.
Upvotes: 1