Pages: [1]   Go Down
Author Topic: halloween project with button  (Read 604 times)
0 Members and 1 Guest are viewing this topic.
uk
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//    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

}
« Last Edit: October 14, 2012, 03:14:03 pm by markthespark » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

uk
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

uk
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

so where do i go for a sketch for it
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: