Purpose:
2 settings, switched by button:
- Lights on - delay - 'heart beat' a few times
- Individual letters flash - all flash together.
This is my first attempt at writing anything, and I know its super basic, but I'm hoping to get some feedback...
The background: my girlfriend hates that I got an Arduino for christmas. Basically she was livid because she knew I wouldn't pay attention to her anymore. In order to be able to freely play, without her scorn, she said that I had to have my first project (due 1 day after opening the box) be something to win her affection. Due a complete lack of experience in all things Arduino-esque, this is the best I could come up with.
Comments, critiques, and improvements are much appreciated.
I feel like there has to be a better way of doing what I wrote. Is there a way to get around the different counters and if statements?
Also, is there a way the button can switch through multiple modes (1 -> 2 -> 3 -> 1 again instead of just 1 -> 2 -> 1...)?
/* Blinking I - HEART - U
This program is to blink an 'I - heart - U' message in two styles:
1 - All letters on for a few seconds, then the heart fades in and out, repeat
2 - Flashes through all letters individually 3 times, then blinks all together.
SETUP: LEDs on breadboard all have one output for all positive leads for all
LEDs for each letter (I from 11, H(eart) from 10, U from 9), and
all negative leads going to the ground band on the bottom
of the breadboard, which goes to GND on makerShield.
Output from 11, 10, 9 should go through resistor before going to bread-
board and LEDs, but green hardly lit up this way so I skipped
the resistor.
**IS THIS BAD? I HAVEN"T BURNED DOWN MY HOUSE YET, SO IT MUST
NOT BE TERRIBLE, BUT I DON"T GET WHY I HAD TO DO THIS...**
*/
int buttonPin = 4; //Input from D4 to resistor (which goes to
// ground) and to a button. Other side of button
// goes to 5V
int state = 0; //Used for button. See button tutorial for more
int I = 11; //DigitalOut 11 = the Letter I
int H = 10; // ditto-ish...
int U = 9; // ditto-ish...
int val = 0; //Used with button to switch blink modes
int old_val = 0; //This will tell if a change happened during button
int GLOW = 100; //Initial value of how bright H will be
int change = 1; //how much the glow will change by
int count = 0; //Used to delay going from H on to H fading
int blinkcount = 0; //counts number of times H fades in and out
int flashcount = 0; //counts number of times IHU has flashed through
void setup() {
pinMode(I,OUTPUT); // I
pinMode(H,OUTPUT); // HEART
pinMode(U,OUTPUT); // U
pinMode(buttonPin, INPUT);
Serial.begin(9600); //Lets us see in SerialMonitor the count/blink
}
void loop() {
val = digitalRead(buttonPin);
if ((val == HIGH) && (old_val == LOW)) { //if button down, turn on
state = 1 - state;
delay(10);
}
old_val = val;
if (state == 1) { //If button has been pressed...
//IHU will blink each letter individually
digitalWrite(I,HIGH); //I...
delay(150);
digitalWrite(I,LOW);
delay(100);
digitalWrite(H,HIGH); //H...
delay(150);
digitalWrite(H,LOW);
delay(100);
digitalWrite(U,HIGH); //U...
delay(150);
digitalWrite(U,LOW);
delay(100);
flashcount = flashcount + 1; //Increases the flash counter
if (flashcount >= 4) { //Once it has flashed through 4 times...
digitalWrite(I, HIGH); //It flashes all letters simultaneously...
digitalWrite(H, HIGH);
digitalWrite(U, HIGH);
delay(200); //Once...
digitalWrite(I, LOW);
digitalWrite(H, LOW);
digitalWrite(U, LOW);
delay(50);
digitalWrite(I, HIGH);
digitalWrite(H, HIGH);
digitalWrite(U, HIGH);
delay(200); //Twice...
digitalWrite(I, LOW);
digitalWrite(H, LOW);
digitalWrite(U, LOW);
delay(50);
digitalWrite(I, HIGH);
digitalWrite(H, HIGH);
digitalWrite(U, HIGH);
delay(200); //Thrice...
digitalWrite(I, LOW);
digitalWrite(H, LOW);
digitalWrite(U, LOW);
delay(50);
flashcount = 0;} //Resets flashCount, so back to the top
}
else { //If button HASN"T been pressed...
digitalWrite(I, HIGH); //I on full blast
digitalWrite(U, HIGH); //U on full blast
count = count + 1; //This count will buy us some time before
//the heart starts to 'beat'
Serial.print(blinkcount); Serial.print(" - "); Serial.println(count);
delay(1); //This shows us how many blinks have happened and the count.
//I was having trouble with the stuff that follows, so
//it helped me know where my counters were.
if (count < 500) {analogWrite(H, 100);} /*Until count builds up to 500
the H is on full blast*/
else //Once count is >= 500, the heart starts to beat
{
analogWrite(H, GLOW);
GLOW = GLOW + change;
delay(1);
if (GLOW == 110) /*Once H is plenty bright (even though max = 255),
the increment that the glow is increasing by (change)
will switch to be negative, thus counting down.
Also, the blink counter will increase by one*/
{delay(100); change = - change;
blinkcount = blinkcount +1;}
if (GLOW == 0) /*Once H is totally dim, switch change value again */
{delay(100); change = - change;}
if (blinkcount > 3 && count > 1000) /*Once we've blinked three times,
and enough time has elapsed for count to
be >1000, blink counter and count get reset
which means count <500, which means
its no longer pulsing, and back to just
being on. */
{blinkcount = 0; count = 0;}
}
}
}
/* Thats it! This is my first time trying anything on my own, and besides the
few tutorials in the GETTING STARTED book, I haven't read much of anything
else, so I apologize if the commentary didn't follow typical fashion.
Comments, critiques, and improvements welcome!*/