Go Down

Topic: halloween project with button (Read 818 times) previous topic - next topic

markthespark

Oct 14, 2012, 10:03 pm Last Edit: Oct 14, 2012, 10:14 pm by markthespark Reason: 1
hi all i'm making a halloween project
when a button is pressed on my door which is connected to my arduino i want it to start a sketch for lightning
i have the sketch but what i want is how to make the sketch run via a button ( code )
any help appreciated
cheers
marko


Code: [Select]
//    FILE: lightning.pde
//  AUTHOR: Rob Tillaart
//    DATE: 2012-05-08
//
// PUPROSE: simulate lighning POC
//
//
#define BETWEEN 2579
#define DURATION 43
#define TIMES 7

#define LEDPIN 13

unsigned long lastTime = 0;
int waitTime = 0;

void setup()
{
  Serial.begin(115200);
  Serial.println("lightning 0.0");

  pinMode(LEDPIN, OUTPUT);
}

void loop()
{
  if (millis() - waitTime > lastTime)  // time for a new flash
  {
    // adjust timing params
    lastTime += waitTime;
    waitTime = random(BETWEEN);

    for (int i=0; i< random(TIMES); i++)
    {
      Serial.println(millis());
      digitalWrite(LEDPIN, HIGH);
      delay(20 + random(DURATION));
      digitalWrite(LEDPIN, LOW);
      delay(10);
    }
  }
 
  // do other stuff here

}

PaulS

How is the button connected to the Arduino? Heavy duty cotton thread? Nylon thread? Perhaps using a switch would work better.

So, how is the switch connected to the Arduino? Where are you reading the switch state?

If you connect one side of the switch to a digital pin and the other side to ground, and enable the pullup resistor (digitalWrite(switchPin, HIGH); after the pinMode(switchPin, INPUT); statement), then, digitalRead(switchPin) will return LOW when the switch is pressed.

A simple if statement will then wrap the existing if statement, and cause the effects to begin when the switch is pressed.

markthespark

the arduino is connected to a 9 volt power supply
but what i want is a momentary swich connected to the arduino to start the sketch

PaulS

Quote
the arduino is connected to a 9 volt power supply
but what i want is a momentary swich connected to the arduino to start the sketch

OK. You can do that.

markthespark

so where do i go for a sketch for it

PaulS

Quote
so where do i go for a sketch for it

Your favorite text editor. There is a call to set the pin mode, a call to enable the pullup resistor, and a call to read the switch state. I showed all those earlier.

Then, all that is needed is an if statement to enclose the existing code. A total of 6 lines of code.

Try something. It isn't that difficult.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy