Go Down

Topic: Basic LED Blink - Arduino VS. Girlfriend. (Read 2217 times) previous topic - next topic

Felp

Purpose:
2 settings, switched by button:
1) Lights on - delay - 'heart beat' a few times
2) 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...)?


Code: [Select]

/* 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!*/

Felp

http://www.youtube.com/watch?v=AodS6yYqYcs

Couldn't include this since the last post was my first ever...

durn spammers...

retrolefty

#2
Dec 30, 2010, 07:03 am Last Edit: Dec 31, 2010, 01:10 am by retrolefty Reason: 1
Cool. For your next move on your GF, add a buzzer and add code have it send out I love you in morse code. There are a couple of morse code software libraries around these parts somewhere. She will be so impressed.   ;)

Quote
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...**


Yes it is bad, you can easily burn out a digital output pin by not having a current limiting resistors. The reason that green gave you a problem is that green leds require around twice the voltage as red leds do. The solution should have been to use a resistor with half the ohm values that the other leds used. Or take two of the original resistors used and wire them in parallel, that gives you half the ohms.

Lefty

Singularity42

so was she adequately impressed?

p.s. My wife bought me an arduino for xmas ;P

daveg360

@Lefty - women around your parts are impressed by very different things to the ones around here!
If your system involves lethal voltages/life critical/flamable elements - you probably shouldn't need to ask.
The Arduino != PC.

retrolefty

Quote
@Lefty - women around your parts are impressed by very different things to the ones around here!


I guess my sarcasm didn't come through.  ;)

daveg360

My apologies - in the absence of facial expressions and vocal nuances, I tend to glance at the posters geographic location to make the judgement call :P

Make her a a big heart that lights up when she has recieved an email from you.  The Arduino email notification thing has been done a million times so there are plenty of write ups.
If your system involves lethal voltages/life critical/flamable elements - you probably shouldn't need to ask.
The Arduino != PC.

retrolefty

#7
Dec 31, 2010, 01:09 am Last Edit: Dec 31, 2010, 01:10 am by retrolefty Reason: 1
;)  ;)  ;)  ;)  means I'm just kidding with you around these parts partner. Got to go water the horse. ;)

daveg360

Ah right gotcha.  Thought it was a squint from standin around in all that there sunshine y'all have  ;)
If your system involves lethal voltages/life critical/flamable elements - you probably shouldn't need to ask.
The Arduino != PC.

thegeekway

Not much humour in Chester from what I remember ;)

But then again Ellesmere Port isnt much better ;)

Go Up