So i just got my first Arduino Board in the other day and i have been playing. I got the protoshield with it from adafruit and so far i have been having a lot of fun with it. I wrote my first program this morning that will eventually turn into a project on my truck. It will control some LED's behind the door handles on my truck.
It's going to be one of those 'just because' projects but i think it will look cool.
Anyway, i would really apreciate it if you could give me some opinons on my coding, there may be a more elegant way to do what i want... but my coding expierence is limited to the last 3 or 4 days so this is what i came up with.
/* This program is a rough draft of one that will eventually power 6 LED's
that will fade in and out when a vehicle alarm system is armed and when
the vehicle's interior lights come on it will stop the Blue pulse and fade
into a green LED
The LED's are the RGB ones available from adafruit industries. */
int Blue = 6; //Just two LEDs for testing
int Green = 5;
int Pwr = 4; //using two digital pins for power to clean up the wiring on the breadboard
int Pwr2 = 8;
int button = 11; //button to simulate alarm system's 'Ground when armed'
int input2 = 12; //button to simulate trucks interior lighting
int pullUp = 1; //Pull up pins for buttons
int pullUp2 = 0;
void setup () {
pinMode (Blue, OUTPUT); //Just two LEDs for testing
pinMode (Green, OUTPUT);
digitalWrite (Blue, HIGH);
digitalWrite (Green, HIGH);
pinMode (Pwr, OUTPUT);//using two digital pins for power to clean up the wiring on the breadboard
pinMode (Pwr2, OUTPUT);
digitalWrite (Pwr, HIGH);
digitalWrite (Pwr2, HIGH);
pinMode (button, INPUT); //button to simulate alarm system's 'Ground when armed'
pinMode (input2, INPUT); //button to simulate trucks interior lighting
pinMode (pullUp, INPUT); //Pull up pins for buttons
pinMode (pullUp2, INPUT);
digitalWrite (pullUp, HIGH);
digitalWrite (pullUp2, HIGH);
}
void loop () {
int btInput = digitalRead(button); //Check Buttons
int input2st = digitalRead(input2);
if (btInput == LOW) { //if alarm is armed go to blue fade cycle
alarmFade ();
}
else if (input2st == LOW) { //if dome lights are on go to turn green LED
domeLight ();
}
}
void alarmFade () {
for(int fadeValueB = 255 ; fadeValueB >=0; fadeValueB-=4) { // fade in from off to on
analogWrite(Blue, fadeValueB); //write to Blue LED pin
delay(50); //Slow Fade
}
int input2st = digitalRead(input2); //Check dome light pin in case of a change
if (input2st == LOW) { //if dome light is on go to proper loop
domeLight ();
}
for(int fadeValue = 0 ; fadeValue <=255 ; fadeValue +=5) { // fade out from on to off
analogWrite(Blue, fadeValue); //write to Blue LED pin
delay(40); //Slow Fade
}
input2st = digitalRead(input2); //Check dome light pin in case of a change
if (input2st == LOW) { //if dome light is on go to proper loop
domeLight ();
}
delay (1500); //pause with LED off for 1.5 seconds
input2st = digitalRead(input2); //Check dome light pin in case of a change
if (input2st == LOW) { //if dome light is on go to proper loop
domeLight ();
}
}
void domeLight () {
int btInput = digitalRead(button); //Check buttons for a change
int input2st = digitalRead(input2);
if (btInput == HIGH && input2st == LOW){ //if alarm is off and dome light is on turn on green led
for(int fadeValueG = 255; fadeValueG >=0; fadeValueG -=5) { // fade in from off to on
analogWrite(Green, fadeValueG); //write to Green LED pin
delay(30); //Fade
}
while (btInput == HIGH && input2st == LOW) { //maintain the Green LED being on until either th alarm is armed or the dome light turns off
digitalWrite (Green, LOW);
btInput = digitalRead(button);
input2st = digitalRead(input2);
}
for(int fadeValueG = 0; fadeValueG <=255 ; fadeValueG +=5) { //Fade out from on to off
analogWrite(Green, fadeValueG); // write to Green LED pin
delay(30);//Fade
}
}
}