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:
/*
* 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:
/*
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:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.