Hello all, so i am very new to arduino and have some questions about this little code i made and why it functions this way. below is the code and yes it is probably not clean and please if you have a slicker/better way to do this then let me know...
int button = 0;
int n=2;
void setup(){
pinMode(9, INPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
}
void loop(){
analogWrite(2,1023);
analogWrite(3,1023);
analogWrite(4,1023);
analogWrite(5,1023);
analogWrite(6,1023);
analogWrite(7,1023);
analogWrite(8,1023);
button = digitalRead(9);
if(button == LOW){
n++;
}
if ( n >8 )
n = 2;
analogWrite(n,0);
delay(150);
}
basically all i'm doing is testing the led and when i send the code to the arduino the top segment lights up, when i press the pushbutton switch it just increments the variable and then lights up the next segment and if you hold the button down it just cycles through the segments with that short delay. it all works fine but I was confused at first about about why it worked the way it did. When I first made the code I did not initialize all values to 1023 and all segments of the led were on. Is this just because it is common anode and not common cathode.?? is this why it seems people don't like common anode led units.?? I didn't think a negative value or lets say a zero value would light the unit up but I guess that's how it works with anode displays. If this were a common cathode unit then would the code be able to be much shorter by not initializing everything to 1023.?? Any info would be great, thanks
E.