system
March 31, 2013, 2:24pm
1
Hello people:)
I'm new to arduino and programming and I just started playing with led's through Arduino Duemilanove.
I wrote the code by my logic and my basic skills for programming.
int led1=2;
int led2=3;
int led3=4;
int led4=5;
int led5=6;
int led6=7;
int led7=8;
int led8=9;
int timer=50;
void setup(){
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
pinMode(led5,OUTPUT);
pinMode(led6,OUTPUT);
pinMode(led7,OUTPUT);
pinMode(led8,OUTPUT);
}
void loop(){
digitalWrite(led1,HIGH);
delay(timer);
digitalWrite(led2,HIGH);
delay(timer);
digitalWrite(led3,HIGH);
delay(timer);
digitalWrite(led4,HIGH);
delay(timer);
digitalWrite(led5,HIGH);
delay(timer);
digitalWrite(led6,HIGH);
delay(timer);
digitalWrite(led7,HIGH);
delay(timer);
digitalWrite(led8,HIGH);
delay(timer);
digitalWrite(led1,LOW);
delay(timer);
digitalWrite(led2,LOW);
delay(timer);
digitalWrite(led3,LOW);
delay(timer);
digitalWrite(led4,LOW);
delay(timer);
digitalWrite(led5,LOW);
delay(timer);
digitalWrite(led6,LOW);
delay(timer);
digitalWrite(led7,LOW);
delay(timer);
digitalWrite(led8,LOW);
delay(timer);
}
I want to write this using minimum coding but I don't know how.
I don't want to have so much "int" and "pinMode" commands.
SO my final question is how can I minimize all of this code?
Thank you.
P.S. Sorry for my bad english.
system
March 31, 2013, 2:27pm
2
system
March 31, 2013, 2:45pm
3
Thank you.I will try working with this.I suppose i will figure it out:)
system
March 31, 2013, 2:53pm
4
const byte led[8] = { 2,3, 4, 5, 6, 7, 8, 9 };
const byte timer=50;
void setup()
{
byte i;
for (i=0; i<8; i++) {
pinMode(led[i], OUTPUT);
}
}
void loop()
{
byte i;
for (i=0; i<8; i++) {
digitalWrite(led[i], HIGH);
delay(timer);
}
for (i=0; i<8; i++) {
digitalWrite(led[i], LOW);
delay(timer);
}
}
system
March 31, 2013, 2:55pm
5
I want to understand it first then work with it but I don't understand it.Unfortunately.
Try doing some reading in the References page for better understanding.
For:Next loop:
http://arduino.cc/en/Reference/For
Array:
http://arduino.cc/en/Reference/Array
increment:
http://arduino.cc/en/Reference/Increment
(i++ is same as i=i+1, which I normally use, with much derision from the real programmers 8) )
Their are literally dozens of ways to do this. Something a little more adaptive to changing arrays sizes -
#define ARRAY_ELEMENTS(ARRAY) (sizeof(ARRAY) / sizeof(ARRAY[0]))
const uint8_t pinsLEDS[] = { 2, 3, 4, 5, 6, 7, 8, 9 };
const unsigned long timer = 50UL;
void setup()
{
for ( int element = ARRAY_ELEMENTS(pinsLEDS); element--; )
{
pinMode(pinsLEDS[element], OUTPUT);
digitalWrite(pinsLEDS[element], LOW);
}
}
void loop()
{
for ( int loop_count = 2; loop_count--; )
{
for ( int element = ARRAY_ELEMENTS(pinsLEDS); element--; )
{
digitalWrite(pinsLEDS[element], !digitalRead(pinsLEDS[element]));
delay(timer);
}
}
}