Arduino Project #1: Pattern repeating LED
by nakubu at 2014-12-10 19:50:04

So I got my Arduino Duemilanove earlier this week, and I've been completely stoked. After getting blinky going, I promptly took out my breadboard from the plastic container, assembled a jumble of jumper wires, resistors, LED and buttons into what became an on-off switch for the LED. Fascinating. Bloody gorgeous. What next?

Well, since I have the button presses as digital input, and the LED as digital out, I thought a neat project would be to record a 3 second button input pattern, and replay that pattern through the LED. That would familiarize me with some of the Arduino's code, and would just be really fun to see live.

For the LED, I connected a jumper cable from the digital out (pin 11) to the breadboard. Then I connected a 1k ohm resistor to the LED, then a jumper cable from the LED to ground. For the next part of the circuit, I connected a jumper cable from a digital read slot (pin 2) on the Arduino the breadboard. I then added a pull-down resistor of 2k ohms from the jumper cable to ground, and lastly, I connected the 5V slot to the button via another jumper cable. This mini-project took about 1-2 hours (coding, after a day of coding, can be tiresome on the ol' noggin), followed by 1 hour of button pushing in awe :)

In any case, I've included a video of it, and the code. Word of warning about the code: It can definitely be done more efficiently (ex: arbitrarily large arrays = fail), I am a computer science major after all, but I also have better things to do:P I haven't really looked at the Arduino API, and it's pretty much just hacked together. Anyway, here's a video, a sketch of the circuits, and the code:





Code:



// set pin numbers:
const int buttonPin = 7; // the number of the pushbutton pin
const int ledPin = 11; // the number of the LED pin

// Variables will change:
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin

// 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
int hold = 0; //keeps state of the button
int beeps = 0; //number of button pushes
int start = 0; // are we starting a new pattern?
long startTime = 0;
long duration = 3000; //duration of the recording
long delayArray[200]; //no dynamic arrays! randomly large number. shut up.

void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, 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 != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonState = reading;
if (reading == LOW){
hold = 0; //otherwise, we're still holding the button!
}
if(hold == 0){ //if the button was let go
if( (start == 1))
{
long time = millis();
if ((time - startTime) < duration ){
if (buttonState == HIGH){
beeps++;
delayArray[beeps] = time;
reading = LOW;
hold = 1;
}
}
else{

//replay the signals on the led
int i = 0;
long lastTime = delayArray[0];
long now, next, span;
digitalWrite(ledPin, HIGH);
delay(150);
digitalWrite(ledPin, LOW);
for(i=1; i<=beeps; ++i){
next = delayArray[i];
now = millis();
span = now + (next-lastTime);
while( (span - now) > 0){
now = millis();
}
digitalWrite(ledPin, HIGH);
delay(150);
digitalWrite(ledPin, LOW);
lastTime = next;
}
start = 0;
beeps = 0;
}
}
else{
beeps = 0;
if(buttonState == HIGH){
start = 1;
startTime = millis();
delayArray[0] = startTime;
reading = LOW;
hold = 1;
}
}
}
}

// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}

Related tags:
arduino, beginner, electronics, circuitry, programming

John Hammink says:
2012-01-01 21:28:36

By way of getting interested in what's been happening in Marionette space (having just got my first b2g building on the friday before new years), naturally I've been soaking up the blogs of yourself and jgriffin and I must say, this arduino looks like an interesting learning project! Anyway, hope you're had an...er...eventful...new year and look forward to talking to you again over b2g, marionette automation (finally) etc.... :) .john


Johnny Ferguson says:
2010-03-30 17:02:32

I lurv videos. That's a cool idea. I'm going to have to try making something like this myself.

One idea I have is to keep it playing back while you record new sequences. One way I could imagine getting around the array limitation is to store the blinks as a state + duration. You would be able to record far longer sequences without too large of an array. Would be super cool if you could have multiple LEDs sync'd to a common clock. It would be a sort of interactive light show/decoration.

COOL BEANS!



name (required):

email:

url:

comment:


Find the squirrels below!