/* Stop-Watch Push button re-start the timer Thanks to coder Alexandre Marques for the countdown timer code https://gist.github.com/xnmarques/879deab9696a0be3cc97 */ int a = 5; int b = 6; int c = 7; int d = 8; int e = A1; int f = A2; int g = A3; int p = 0; // NOTE: Sketch will return an error message while pin 0 and pin 1 are plugged in. Unplug pins to upload, then replug in. int d4 = 4; int d3 = 3; int d2 = 2; int d1 = 1;// NOTE: Sketch will return an error message while pin 1 and pin 0 are plugged in. Unplug pins to upload, then replug in. int LaunchLED = 9; //light up led for rocketlaunch int AudioActivate = A4; //the recordable greeting card chip is atttached here. I used this one: http://www.amazon.com/gp/product/B006NFHLLW/ int buttonPin = A5; //button pin int buttonPushCounter = 0; // counter for the number of button presses int buttonState = 0; // current state of the button int lastButtonState =0; // previous state of the button // This variable gets toggled either high or low each time the button is pressed. // In other words, this variable changes states with each button press. bool pressed = true; int buttonpress; long n = 7500; // 15000 start time: 300000 = 30s seconds; was 15000 but cut in half to speed it up and restart to 15 seconds int x = 50; //was 100, but cut in half to speed up int del = 55; //fade integers for the launch LED // int brightness = 0; // how bright the LED is //int fadeAmount = 5; // how many points to fade the LED by //BELOW is the code for the bargraph. /* SparkFun Bargraph Breakout example sketch Mike Grusin, SparkFun Electronics This sketch shows how to use the SFEbarGraph library to send data to SparkFun's Bargraph Breakout board Hardware connections: Connect the following pins on the bargraph board to your Arduino: Name Function Uno pin Mega pin +5V Power supply 5V 5V GND Ground GND GND SIN SPI input 11 (MOSI) 51 (MOSI) CLK Clock input 13 (SCK) 52 (SCK) LAT Latch input 10 (SS) 53 (SS) License: This code is free to use, change, improve, even sell! All we ask for is two things: 1. That you give SparkFun credit for the original code, 2. If you sell or give it away, you do so under the same license so others can do the same thing. More at http://creativecommons.org/licenses/by-sa/3.0/ Have fun! -your friends at SparkFun */ // Step one: include both the SFEbarGraph and SPI libraries #include #include // Step two: create a bargraph object; we call it BG in this example but you can call it whatever you wish // (use the same name in front of all the library functions) SFEbarGraph BG; // Some of the math we're doing in this example requires the number of bargraph boards // you have connected together (normally this is one, but you can have a maximum of 8). const int numbargraphs = 1; //added this from http://www.arduino.cc/en/Tutorial/BarGraph const int analogPin = A0; // the pin that the potentiometer is attached to void setup() { // Let's initialize the bargraph library! // You can call BG.begin() several ways: // Without parameters, it defaults to one bargraph board and LATch pin set to 10 on Uno, or 53 on Mega. // You can also call it with the number of daisy-chained boards (1-8, default 1), using the default LATch pin. // Or you can call it with both the number of boards and the LATch pin you wish to use. // For two bargraphs and pin 9 for the latch pin, call BG.begin(2,9); BG.begin(numbargraphs); //above is the only setup needed for the bargraph pinMode(d1, OUTPUT); pinMode(d2, OUTPUT); pinMode(d3, OUTPUT); pinMode(d4, OUTPUT); pinMode(a, OUTPUT); pinMode(b, OUTPUT); pinMode(c, OUTPUT); pinMode(d, OUTPUT); pinMode(e, OUTPUT); pinMode(f, OUTPUT); pinMode(g, OUTPUT); pinMode(p, OUTPUT); //launch mode light output pinMode(LaunchLED, OUTPUT); pinMode(AudioActivate, OUTPUT); pinMode(buttonPin, INPUT); //button input pinMode(analogPin, INPUT); //pot pin? } void loop() { unsigned long endtime; //part of bargraph //Below is the countdown code clearLEDs(); pickDigit(1); pickNumber((n/x/1000)%10); delayMicroseconds(del); clearLEDs(); pickDigit(2);//2 pickNumber((n/x/100)%10); delayMicroseconds(del); clearLEDs(); pickDigit(3);//3 dispDec(3); pickNumber((n/x/10)%10); delayMicroseconds(del); clearLEDs(); pickDigit(4);//4 pickNumber(n/x%10); delayMicroseconds(del); n--; //'n++' for stopwatch //I don't really get the button code here, but it does what it is supposed to! // read the pushbutton input pin: buttonState = digitalRead(buttonPin); // compare the buttonState to its previous state if (buttonState != lastButtonState) { // if the state has changed, increment the counter if (buttonState == LOW) { // if the current state is HIGH then the button // wend from off to on: buttonPushCounter++; pressed = !pressed; //toggle state of the variable //digitalWrite(buttonPin, HIGH); // }else { digitalWrite(buttonPin, LOW); } } // save the current state as the last state, //for next time through the loop lastButtonState = buttonState; // reset timer? every four button pushes by // checking the modulo of the button push counter. // the modulo function gives you the remainder of // the division of two numbers: if (buttonPushCounter % 4 == 0) { digitalWrite(buttonPin, LOW); } else { digitalWrite(buttonPin, HIGH); } // will this work? // this is for the audio activation of the hacked recordable holiday card if (n == 0) //indicating timer is at T minus 000.0 { analogWrite (AudioActivate, 200); // seems to work! Sends a pulse to simulate button press -- is the other wire just going to gnd? delay(50); // to make sure there is enough time for the "button press" analogWrite (AudioActivate, 0);//need to send it low right away, since a second press turns it off } else { digitalWrite (AudioActivate, LOW); } // //attempting to activate Launch LED on 000.0 //super clunky solution for fading the led if (n == 0){ analogWrite (LaunchLED, 50); delay (250); analogWrite (LaunchLED, 150); delay (25); analogWrite (LaunchLED, 200); delay (250); analogWrite (LaunchLED, 225); delay (250); analogWrite (LaunchLED, 255); delay (250); analogWrite (LaunchLED, 225); delay (250); analogWrite (LaunchLED, 200); delay (25); analogWrite (LaunchLED, 150); delay (250); analogWrite (LaunchLED, 50); delay (250); analogWrite (LaunchLED, 150); delay (25); analogWrite (LaunchLED, 200); delay (250); analogWrite (LaunchLED, 225); delay (250); analogWrite (LaunchLED, 255); delay (250); analogWrite (LaunchLED, 225); delay (250); analogWrite (LaunchLED, 200); delay (25); analogWrite (LaunchLED, 150); delay (250); analogWrite (LaunchLED, 50); delay (250); analogWrite (LaunchLED, 150); delay (25); analogWrite (LaunchLED, 200); delay (250); analogWrite (LaunchLED, 225); delay (250); analogWrite (LaunchLED, 255); delay (250); analogWrite (LaunchLED, 225); delay (250); analogWrite (LaunchLED, 200); delay (25); analogWrite (LaunchLED, 150); delay (250); analogWrite (LaunchLED, 50); delay (250); analogWrite (LaunchLED, 100); // analogWrite (LaunchLED, 150); delay (25); analogWrite (LaunchLED, 200); delay (250); analogWrite (LaunchLED, 225); delay (250); analogWrite (LaunchLED, 255); delay (250); analogWrite (LaunchLED, 225); delay (250); analogWrite (LaunchLED, 200); delay (25); analogWrite (LaunchLED, 150); delay (250); analogWrite (LaunchLED, 50); delay (250); /* //would this work? for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { // sets the value (range from 0 to 255): analogWrite(LaunchLED, fadeValue); // wait for 30 milliseconds to see the dimming effect delay(30); } // fade out from max to min in increments of 5 points: for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { // sets the value (range from 0 to 255): analogWrite(LaunchLED, fadeValue); // wait for 30 milliseconds to see the dimming effect delay(30); } */ } else { digitalWrite (LaunchLED, LOW); } if (pressed) //(buttonState == LOW) { n = 7500; //re-start time: 200000 = 20s seconds; was 15000 but cut in half to speed it up and restart to 15 seconds } //putting bargraph code here //pot code // read the potentiometer: int sensorReading = analogRead(analogPin); // map the result to a range from 0 to the number of LEDs: const int ledLevel = map(sensorReading, 0, 800, 0, (numbargraphs*30)); // Trying to make "bugs" if power turned up too high. // BASIC BARGRAPH! // Turn all LEDs on then off using BG.barGraph() // BG.BarGraph() takes two values, one for the "bar" (how many LEDs to light), // and a separate floating "peak" which lights up one LED. // When you call BG.barGraph(), the values are immediately displayed on the LEDs. // Numbering starts at 1, 0 = all LEDs off. BG.barGraph(ledLevel,0); //simply plug in the variable from the pot et voila! //call up makebugs routine when the power level is 'overloaded' if (sensorReading>1000) { makebugs(); } else { regBG();} } //end bargraph code void pickDigit(int x) { digitalWrite(d1, HIGH); digitalWrite(d2, HIGH); digitalWrite(d3, HIGH); digitalWrite(d4, HIGH); switch(x) { case 1: digitalWrite(d1, LOW); break; case 2: digitalWrite(d2, LOW); break; case 3: digitalWrite(d3, LOW); digitalWrite(p, HIGH); //new break; case 4: //this previously said "default" digitalWrite(d4, LOW); break; } } void pickNumber(int x) { switch(x) { default: zero(); break; case 1: one(); break; case 2: two(); break; case 3: three(); break; case 4: four(); break; case 5: five(); break; case 6: six(); break; case 7: seven(); break; case 8: eight(); break; case 9: nine(); break; case 10: letterE(); break; case 11: letterR(); break; case 12: letterO(); break; case 13: letterB(); break; case 14: letterU(); break; case 15: letterG(); } } void dispDec(int x) { digitalWrite(p, LOW); } void clearLEDs() { digitalWrite(a, LOW); digitalWrite(b, LOW); digitalWrite(c, LOW); digitalWrite(d, LOW); digitalWrite(e, LOW); digitalWrite(f, LOW); digitalWrite(g, LOW); digitalWrite(p, LOW); } void zero() { digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, HIGH); digitalWrite(g, LOW); } void one() { digitalWrite(a, LOW); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, LOW); digitalWrite(e, LOW); digitalWrite(f, LOW); digitalWrite(g, LOW); } void two() { digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, LOW); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, LOW); digitalWrite(g, HIGH); } void three() { digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, LOW); digitalWrite(f, LOW); digitalWrite(g, HIGH); } void four() { digitalWrite(a, LOW); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, LOW); digitalWrite(e, LOW); digitalWrite(f, HIGH); digitalWrite(g, HIGH); } void five() { digitalWrite(a, HIGH); digitalWrite(b, LOW); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, LOW); digitalWrite(f, HIGH); digitalWrite(g, HIGH); } void six() { digitalWrite(a, HIGH); digitalWrite(b, LOW); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, HIGH); digitalWrite(g, HIGH); } void seven() { digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, LOW); digitalWrite(e, LOW); digitalWrite(f, LOW); digitalWrite(g, LOW); } void eight() { digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, HIGH); digitalWrite(g, HIGH); } void nine() { digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, LOW); digitalWrite(f, HIGH); digitalWrite(g, HIGH); } void letterE() { digitalWrite(a, HIGH); digitalWrite(b, LOW); digitalWrite(c, LOW); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, HIGH); digitalWrite(g, HIGH); } void letterR() { digitalWrite(a, LOW); digitalWrite(b, LOW); digitalWrite(c, LOW); digitalWrite(d, LOW); digitalWrite(e, HIGH); digitalWrite(f, LOW); digitalWrite(g, HIGH); } void letterO() { digitalWrite(a, LOW); digitalWrite(b, LOW); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, LOW); digitalWrite(g, HIGH); } void letterB() { digitalWrite(a, LOW); digitalWrite(b, LOW); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, HIGH); digitalWrite(g, HIGH); digitalWrite(p, LOW); } void letterU() { digitalWrite(a, LOW); digitalWrite(b, LOW); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, LOW); digitalWrite(g, LOW); digitalWrite(p, LOW); } void letterG() { digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, LOW); digitalWrite(f, HIGH); digitalWrite(g, HIGH); digitalWrite(p, LOW); } void makebugs() { Error (); // DANCING SINE BUGS! Also from Sparkfun unsigned long endtime; //part of bargraph // Paint 8 moving bugs onto the bargraph, and watch them dance int sensorReading = analogRead(analogPin); const int ledLevel = map(sensorReading, 0, 800, 0, (numbargraphs*30)); // Create 8 bugs float bug[8]; int x; // Zero them all for(x=0; x<8; x++) bug[x] = 0.0; // Do this for 30 seconds endtime = millis() + 30000; while (millis() < endtime) //while (millis() >0) //this goes forever //while (sensorReading >=990) { // Clear the canvas // if (sensorReading <=990) { regBG(); } //Nope. BG.clear(); //if (sensorReading <=990) { regBG(); } // this makes the bugs AND the bargraph! // Move each bug along a sine wave at different rates for(x=0; x<8; x++) { bug[x] += ((PI/8000.0) * (x+1)); // if (sensorReading <=990) { regBG(); } //does not work here either BG.paint((numbargraphs*15)+(sin(bug[x])*(numbargraphs * 15)),HIGH); } // Send the canvas to the display // experiment // this resets to regBG after one cycle, but does not interrupt the function if (sensorReading <=750) { regBG(); } BG.send(); } BG.clear(); BG.send(); delay(500); } void regBG() { int sensorReading = analogRead(analogPin); // map the result to a range from 0 to the number of LEDs: const int ledLevel = map(sensorReading, 0, 800, 0, (numbargraphs*30)); //for some reason 670 is the number that works instead of 1023. Trying to make bugs if power turned up too high. // BASIC BARGRAPH! BG.barGraph(ledLevel,0); } void Error() { //mess with LEDs int dSec=10; // 10 is E, 11 is R, 12 is O ("oh") //13 is B, 14 is U, 15 is G, 5 is S //Now I have it saying "BUGS" int q = 0; while(q < 20){ // do something repetitive 20 times q++; pickDigit(1); pickNumber(13); delay (dSec); pickDigit(2); pickNumber(14); delay (dSec); pickDigit(3); pickNumber(15); delay (dSec); pickDigit(4); pickNumber(5); delay (dSec); clearLEDs(); } }