Show Posts
Pages: [1] 2
1  Using Arduino / Programming Questions / Re: Adding an on/off button with an existing sketch on: January 02, 2013, 04:24:41 pm
I just wanted you to know my son got a 100 for his grade on this project. I appreciate your help on this. It looks like he's getting hooked on Arduino!

Bob
2  Using Arduino / Programming Questions / Re: Adding an on/off button with an existing sketch on: December 21, 2012, 02:41:32 pm
Thank you for the help. We couldn't have done it without you!
3  Using Arduino / Programming Questions / Re: Adding an on/off button with an existing sketch on: December 20, 2012, 05:08:57 pm
fkeel,

We completed the nebula project a couple of nights ago and yesterday my 11-year-old son showed it to his class. No grade yet, but my guess is he'll get an A. Certainly the kids and the teacher gasped when he pushed the button. Thanks so much for the help. Here's a YouTube video of it as promised:

http://youtu.be/A9TYzz1qBL8

Thanks again for all the help. He was so happy with it and jumpstarted his interest in learning the code. We're actually thinking we'll make a larger one to put in the fireplace. Much cooler than a fire!

Bob
4  Using Arduino / Motors, Mechanics, and Power / Re: How long will a 9v last with 4 LED's? on: December 18, 2012, 09:56:27 pm
Thanks for the response. Given your comments, I'm going to definitely have him plug it in only at Showtime. How long do you think four LED's will flicker from a 9V. They're more on than off.

Thanks again. This has been a huge help!
5  Using Arduino / Motors, Mechanics, and Power / How long will a 9v last with 4 LED's? on: December 18, 2012, 09:23:44 pm
Me and my 11-year-old son have a built a school project with four LED's connected to an Arduino as well as a button to turn them on and off. Even when the LED's are off, the on-board LED seems to stay lit to indicate it's connected to power. We have them connected to a 9V battery (the little square ones). He's taking it to school in the morning as part of a class project. The four LED's will remain off (but with the on-board LED glowing) until he needs to show the class, no longer than a 7 hour wait.

I'm wondering how long an Arduino can be powered in this "standby" mode and if, when it's his turn to show his project, if it'll still have enough juice to flash the four LED's for 15 minutes or so.

Any help would be appreciated.

Thanks!
6  Using Arduino / General Electronics / Re: Any tricks to keeping pins in breadboard? on: December 14, 2012, 08:02:40 pm
Thanks guys. We really appreciate your suggestions!
7  Using Arduino / General Electronics / Any tricks to keeping pins in breadboard? on: December 14, 2012, 06:37:11 pm
Me and my son are making a diorama he's going to bring into school and using an Uno and a breadboard. He's got a few LED's and a button connected to the breadboard and whenever we move it some wires pop out. Is there a way people lock these wires into the breadboard and even the Arduino so they don't fall out when traveling? Any ideas would be appreciated.

We do have a soldering gun and are going to use that to spread out the LED"s throughout the project, so maybe there's a use for that.

Thanks
8  Using Arduino / Programming Questions / Re: Adding an on/off button with an existing sketch on: December 14, 2012, 05:38:25 pm
Thanks for that last edit. It worked with


if (blinkLEDs == false)
 {
    digitalWrite(ledPin[1] ,LOW);
    digitalWrite(ledPin[2] ,LOW);
    digitalWrite(ledPin[3] ,LOW);
    digitalWrite(ledPin[0] ,LOW); 

Yay! We'll definitely post a video smiley
9  Using Arduino / Programming Questions / Re: Adding an on/off button with an existing sketch on: December 14, 2012, 05:04:29 pm
Thank you!!! This'll help us explore the code. We really appreciate all your help.
10  Using Arduino / Programming Questions / Re: Adding an on/off button with an existing sketch on: December 14, 2012, 05:01:16 pm
Aha! We noticed the uppercase "S" and it worked! THANKS SO MUCH!!!!

One more question. Upon the button press, the lights stop twinkling but still remain on. Would there happen to be an easy change to a variable to make them turn off altogether instead of stopping twinkling?

Honestly, I think he's happy the way it is, but if there's a variable we can change in the code to have the button turn the lights off, we'd love to give it a try?

Thanks!
11  Using Arduino / Programming Questions / Re: Adding an on/off button with an existing sketch on: December 14, 2012, 04:50:00 pm
Hmmm. We're getting an error:

sketch_dec14b.cpp: In function 'void loop()':
sketch_dec14b:58: error: 'blinkLEDS' was not declared in this scope

If it helps, the button code was taken from the second sketch I posted and worked great with in that one. When I tried to move it over to the  twinkle one, that's where the problems seemed to start.

Sorry, I really wasn't looking for someone to write the code for us, but under the circumstances, I can't tell you how much we appreciate it!
12  Using Arduino / Programming Questions / Re: Adding an on/off button with an existing sketch on: December 14, 2012, 04:43:04 pm
Thanks! My son is very excited and appreciative. We'll give it a try. I'll make sure I take him through the code.
13  Using Arduino / Programming Questions / Re: Adding an on/off button with an existing sketch on: December 14, 2012, 04:11:36 pm
Thanks fkeel. I appreciate you cleaning up my code and the explanations. You're right, it does compile without an error. The button still doesn't work, as I'm sure you know, so we're moving through the clues you provided trying to figure out the solution. This may be too beyond us at this point and we're running out of time.

If we can't get the code fixed in time, I'm wondering if there's an alternative way for a button to control this. Perhaps just put it between the battery and the Arduino?

Thanks for your help. If you think it should be working with the code you fixed, please let us know and we'll try to make sure all the pins are in the right places, etc.

Thanks again!
14  Using Arduino / Programming Questions / Re: Adding an on/off button with an existing sketch on: December 13, 2012, 08:10:08 pm
Here's the code after unsuccessfully trying to insert the button code and put it into the random flickering code:


Code:
/*
 * randomly flickering LEDs
 */
const int buttonPin = 2;     // the number of the pushbutton pin
int ledPin[] = {
  13, 9, 10, 11};              // pwm pins only
int ledState[5];                 // last state of each led
long randNumber;

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
    pinMode(buttonPin, INPUT);
  for (int i=0; i<=4; i++){      // set each led pin as an output
    pinMode(ledPin[i], OUTPUT);
  }
  randomSeed(analogRead(0));     // seed the rnd generator with noise from unused pin

  for (int i=0; i<=4; i++){      // init each led with a random value
    ledState[i] = random(20, 201);
  }
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (reading != lastReading) {
    // reset the debouncing timer
    lastDebounceTime = millis();
    // save the reading.  Next time through the loop,
    // it'll be lastReading:
    lastReading = reading;
  }
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so accept the button state change:
  
    // toggle the LED if the state of the button changes from LOW to HIGH:
    if (lastButtonState == LOW && reading == HIGH) {
      if (ledState == HIGH) {
        ledState = LOW;
      } else {
        ledState = HIGH;
      }
      digitalWrite(ledPingreen, ledState);
      digitalWrite(ledPinred, ledState);
      digitalWrite(ledPinblue, ledState);
      digitalWrite(ledPinyellow, ledState);
    }
    lastButtonState = reading;
  }
}


void loop(){
  for (int i=0; i<=4; i++){                  // for each led:
    analogWrite(ledPin[i], ledState[i]);     // set the pwm value of that pin determined previously
    randNumber = random(-40, 41);            // generate new random number and add that to the current value
    ledState[i] += randNumber;               // that range can be tweaked to change the intensity of the flickering
    if (ledState[i] > 200) {                 // clamp the limits of the pwm values so it remains within
      ledState[i] = 200;                     // a pleasing range as well as the pwm range
    }
    if (ledState[i] < 10) {
      ledState[i] = 10;
    }
  }
  delay(100);    // the delay between changes
}



Here are the errors we're getting. My son is only 11 and I'm 51. We're both new to this so we're both struggling. I'm assuming we're not accounting for the difference between the way the button turns on the LED's from LOW to HIGH in the button sketch and the way the random flickering is handled in the other sketch. Any advice would be appreciated. Thanks.


Code:
random_flickering_with_button.cpp: In function 'void loop()':
random_flickering_with_button:35: error: 'lastReading' was not declared in this scope
random_flickering_with_button:48: error: 'lastButtonState' was not declared in this scope
random_flickering_with_button:49: error: ISO C++ forbids comparison between pointer and integer
random_flickering_with_button:50: error: incompatible types in assignment of 'int' to 'int [5]'
random_flickering_with_button:52: error: incompatible types in assignment of 'int' to 'int [5]'
random_flickering_with_button:54: error: 'ledPingreen' was not declared in this scope
random_flickering_with_button:55: error: 'ledPinred' was not declared in this scope
random_flickering_with_button:56: error: 'ledPinblue' was not declared in this scope
random_flickering_with_button:57: error: 'ledPinyellow' was not declared in this scope
random_flickering_with_button:59: error: 'lastButtonState' was not declared in this scope
random_flickering_with_button.cpp: In function 'void loop()':
random_flickering_with_button:64: error: redefinition of 'void loop()'
random_flickering_with_button:26: error: 'void loop()' previously defined here


Moderator edit: [code] [/code] tags added.
15  Using Arduino / Programming Questions / Adding an on/off button with an existing sketch on: December 13, 2012, 08:09:31 pm
I've finally gotten my 11 year-old son interested in the Arduino I bought him a few months back for his birthday because has a great application--a project in school to build a model of a nebula. We've been building one out of fuzzy stuff and inserting LED's.  We've successfully made four LED randomly flicker using the sketch below.

We also have a push button attached we've used in previous projects to turn LED's on and off after each push. Our problem is we're now trying to add  the on/off button part of the first sketch to the random flickering sketch and it's not going well. Any help would be appreciated. I'm going to post three sketches. First, the twinkle sketch, which works great:

Code:
/*
 * randomly flickering LEDs
 */

int ledPin[] = {
  13, 9, 10, 11};              // pwm pins only
int ledState[5];                 // last state of each led
long randNumber;

void setup() {
  for (int i=0; i<=4; i++){      // set each led pin as an output
    pinMode(ledPin[i], OUTPUT);
  }
  randomSeed(analogRead(0));     // seed the rnd generator with noise from unused pin

  for (int i=0; i<=4; i++){      // init each led with a random value
    ledState[i] = random(20, 201);
  }
}

void loop(){
  for (int i=0; i<=4; i++){                  // for each led:
    analogWrite(ledPin[i], ledState[i]);     // set the pwm value of that pin determined previously
    randNumber = random(-40, 41);            // generate new random number and add that to the current value
    ledState[i] += randNumber;               // that range can be tweaked to change the intensity of the flickering
    if (ledState[i] > 200) {                 // clamp the limits of the pwm values so it remains within
      ledState[i] = 200;                     // a pleasing range as well as the pwm range
    }
    if (ledState[i] < 10) {
      ledState[i] = 10;
    }
  }
  delay(100);    // the delay between changes
}

Here's the button sketch we've also gotten to work:


Code:
/*
 Debounce
 
 Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
 press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
 a minimum delay between toggles to debounce the circuit (i.e. to ignore
 noise).  
 
 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached from pin 2 to +5V
 * 10K resistor attached from pin 2 to ground
 
 * Note: On most Arduino boards, there is already an LED on the board
 connected to pin 13, so you don't need any extra components for this example.
 
 
 created 21 November 2006
 by David A. Mellis
 modified 3 Jul 2009
 by Limor Fried
 
 
 http://www.arduino.cc/en/Tutorial/Debounce
 */

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPingreen =  13;      // the number of the LED pin
const int ledPinred =  9;      // the number of the LED pin
const int ledPinblue =  11;      // the number of the LED pin
const int ledPinyellow =  10;      // the number of the LED pin

// Variables will change:
int ledState = LOW;         // the current state of the output pin
int lastButtonState = LOW;   // the previous reading from the input pin
int lastReading= LOW;

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPingreen, OUTPUT);
  pinMode(ledPinred, OUTPUT);
  pinMode(ledPinblue, OUTPUT);
  pinMode(ledPinyellow, OUTPUT);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (reading != lastReading) {
    // reset the debouncing timer
    lastDebounceTime = millis();
    // save the reading.  Next time through the loop,
    // it'll be lastReading:
    lastReading = reading;
  }
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so accept the button state change:
  
    // toggle the LED if the state of the button changes from LOW to HIGH:
    if (lastButtonState == LOW && reading == HIGH) {
      if (ledState == HIGH) {
        ledState = LOW;
      } else {
        ledState = HIGH;
      }
      digitalWrite(ledPingreen, ledState);
      digitalWrite(ledPinred, ledState);
      digitalWrite(ledPinblue, ledState);
      digitalWrite(ledPinyellow, ledState);
    }
    lastButtonState = reading;
  }
}


I seem to be exceeding the 9500 character limit, so I'll put in the next post the code after unsuccessfully trying to insert the button code and put it into the random flickering code.


Moderator edit: [code] [/code] tags added.
Pages: [1] 2