Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: Pause for loop without delay on: January 09, 2013, 04:55:28 pm
Or this from the playground: http://playground.arduino.cc//Code/AvoidDelay
2  Using Arduino / Programming Questions / Use momentary to trigger output for a time w/o using delay? on: December 01, 2012, 08:51:10 pm
Hello, I'm just starting out with this hobby and have tried to familiarize myself with the examples and the forum, but my programming skills are very green.  I have a question regarding using a momentary switch to turn on an LED for a specified time using the "blinkwithoutdelay" method.  I have an Arduino Uno set up with a momentary switch and resistor connected as an input to pin 6 and an LED and resistor connected as an output through a transistor (just wanted to learn how that worked) to pin 3.  What I was attempting from the code below was that upon pressing the momentary switch the LED would turn on for 5 seconds and then turn off.  What is actually happening is that the LED will blink on and off.  Once I've pressed the button and the LED has blinked I can press the button again with no blink.  I then have to wait approximately five seconds until a button press will trigger a blink.  I've also noticed that if I press and hold the momentary switch what I get is a blink then approximately a 5 second wait and then another blink.  May be coincidental?  I've tried many different variations with if, if/else, and while, but I'm stumped. Any help would be appreciated!

Code:
/* Turns on an LED for 5 seconds when a momentary pushbutton is pressed.
 
 The circuit:
 *pin 3 connected to LED through transistor
 *pin 6 connected to pushbutton/resistor */

const int buttonPin = 6;        //the number of the pushbutton pin
const int LEDPin =  3;          //the number of the alarm pin
int buttonState;            //variable for the pushbutton status
unsigned long ontime = 5000;    //variable for the alarm on-time
unsigned long currentMillis;
long previousMillis = 0;

void setup() {
  pinMode(LEDPin, OUTPUT);       //initialize the LED pin as an output
  pinMode(buttonPin, INPUT);     //initialize the pushbutton pin as an input
  Serial.begin(9600);            //initialize serial communication for debugging
}

void loop() {
    
    buttonState = digitalRead(buttonPin);   //check if the pushbutton is pressed
    Serial.println(buttonState);            //print pushbutton condition to serial monitor
    currentMillis = millis();
    if (buttonState == HIGH) {                            //if pushbutton has been pressed:      
       if (currentMillis - previousMillis > ontime) {     //check if difference between current time        
         digitalWrite(LEDPin, HIGH);                      //and previous time is larger than the alarm time
         previousMillis = currentMillis;        
       }
    }
    digitalWrite(LEDPin, LOW);              //turn LED off
}

Eventually I would like to add an on/off switch that could be used to arm/disarm the system and also turn off the LED prior to the 5 second time limit if desired, but we must crawl before we walk. If anyone knows of any examples of how to do this (or something similar to this) I would gladly welcome a link.
3  Topics / Home Automation and Networked Objects / Re: Arduino Controller android app on: November 08, 2012, 11:32:43 pm
Awesome! thanks Marque for posting this.  I don't know if I would have been able to figure this out on my own, especially the Android app (which does NOT have ads).
Pages: [1]