I am new to Arduino and have a project I am working on. I am using a Duemilanove board. The project is as followed:
Switch 1 will activate a motor and LED. These will run until I shut the switch off. (This section works fine)
Switch 2 will activate a motor and LED. These will need to be on for 1 minute then 1 minute off. This loop will need to run for 2 hrs and then shut off completely. regardless of button state.
I can not seem to get the timers working for switch 2. I attempted to start with just a simple millis timer but I can not get it to function at all. Once I got this to function then I would add the timer/loop stop if possible.
Basically I am a tad lost..... I could really use the help please.
Here is my current code with documented notes in it.
/*
FUNCTION
Button 1 will operate motor until shut off with button
Button 2 will operate motor on a timer with end timer after X amount of time has elapsed
Buttons consist of (1) 3 position toggle with center off.
CIRCUIT
10K resistor between pins 2 and GND
10k resistor between pins 3 and GND
Button 1 and 2 attached to +5v
Button 1 = pin 2
Button 2 = pin 3
LED 1 = pin 10
LED 2 = pin 11
Motor 1 = pin 12
Motor 2 = pin 13
TIMER
Motor 2 and LED 2 = 1 minute on then 1 minute off
Timer needs to end and shut the loop down after 2 hrs of run time.
*/
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 2; // the number of the push button pin
const int buttonPin2 = 3; // the number of the push button pin
const int ledPin1 = 10; // the number of the LED pin
const int ledPin2 = 11; // the number of the LED pin
const int motorPin1 = 12; // the number of the MOTOR pin
const int motorPin2 = 13; // the number of the MOTOR pin
const long interval = 1000; // interval at which to LED 2 and Motor 2 activate (milliseconds)
// variables will change:
int buttonState1 = LOW; // variable for reading the push button status
int buttonState2 = LOW; // variable for reading the push button status
int led2State = LOW; // led2State used to set the LED
int motor2State = LOW; // motor2State used to set motor 2
unsigned long previousMillis = 0; // will store last time was updated
void setup() {
// initialize the pins as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
// initialize the push button pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop() {
// read the state of the push button value:
buttonState1 = digitalRead(buttonPin1);
// read the state of the push button value:
buttonState2 = digitalRead(buttonPin2);
// check if the push button is pressed.
// if it is, the button State is HIGH:
if (buttonState1 == HIGH) {
// turn LED 1 + motor 1 on:
digitalWrite(ledPin1, HIGH);
digitalWrite(motorPin1, HIGH);
} else {
// turn LED 1 and motor 1 off:
digitalWrite(ledPin1, LOW);
digitalWrite(motorPin1, LOW);
}
// Motor 2 output + LED 2 on a timer with delay and end after certain time.
// Motor 2 + LED 2 delay should be 1 minute on then 1 minute off
// Total time to run should be 2hrs then shut off loop
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
}
if (buttonState2 == HIGH) {
led2State = HIGH;
motor2State = HIGH;
// turn LED 2 + motor 2 on:
//digitalWrite(ledPin2, HIGH);
//digitalWrite(motorPin2, HIGH);
} else {
led2State = LOW;
motor2State = LOW;
// turn LED 2 and motor 2 off:
//digitalWrite(ledPin2, LOW);
//digitalWrite(motorPin2, LOW);
digitalWrite(ledPin2, led2State);
digitalWrite(motorPin2, motor2State);
}
}
I guess the switches you are using are latching.
You can try something like this:
/*
FUNCTION
Button 1 will operate motor until shut off with button
Button 2 will operate motor on a timer with end timer after X amount of time has elapsed
Buttons consist of (1) 3 position toggle with center off.
CIRCUIT
10K resistor between pins 2 and GND
10k resistor between pins 3 and GND
Button 1 and 2 attached to +5v
Button 1 = pin 2
Button 2 = pin 3
LED 1 = pin 10
LED 2 = pin 11
Motor 1 = pin 12
Motor 2 = pin 13
TIMER
Motor 2 and LED 2 = 1 minute on then 1 minute off
Timer needs to end and shut the loop down after 2 hrs of run time.
*/
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 2; // the number of the push button pin
const int buttonPin2 = 3; // the number of the push button pin
const int ledPin1 = 10; // the number of the LED pin
const int ledPin2 = 11; // the number of the LED pin
const int motorPin1 = 12; // the number of the MOTOR pin
const int motorPin2 = 13; // the number of the MOTOR pin
const long interval = 1000; // interval at which to LED 2 and Motor 2 activate (milliseconds)
// variables will change:
int buttonState1 = LOW; // variable for reading the push button status
int buttonState2 = LOW; // variable for reading the push button status
int led2State = LOW; // led2State used to set the LED
int motor2State = LOW; // motor2State used to set motor 2
unsigned long previousMillis = 0; // will store last time was updated
void setup() {
// initialize the pins as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
// initialize the push button pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop() {
// read the state of the push button value:
buttonState1 = digitalRead(buttonPin1);
// read the state of the push button value:
buttonState2 = digitalRead(buttonPin2);
// check if the push button is pressed.
// if it is, the button State is HIGH:
if (buttonState1 == HIGH) {
// turn LED 1 + motor 1 on:
digitalWrite(ledPin1, HIGH);
digitalWrite(motorPin1, HIGH);
} else {
// turn LED 1 and motor 1 off:
digitalWrite(ledPin1, LOW);
digitalWrite(motorPin1, LOW);
}
// Motor 2 output + LED 2 on a timer with delay and end after certain time.
// Motor 2 + LED 2 delay should be 1 minute on then 1 minute off
// Total time to run should be 2hrs then shut off loop
static bool toggle ;
unsigned long currentMillis= millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
toggle = !toggle ;
}
if (buttonState2 == HIGH && toggle) {
led2State = HIGH;
motor2State = HIGH;
// turn LED 2 + motor 2 on:
//digitalWrite(ledPin2, HIGH);
//digitalWrite(motorPin2, HIGH);
} else {
led2State = LOW;
motor2State = LOW;
// turn LED 2 and motor 2 off:
//digitalWrite(ledPin2, LOW);
//digitalWrite(motorPin2, LOW);
digitalWrite(ledPin2, led2State);
digitalWrite(motorPin2, motor2State);
}
}
Thank you for the response. Yes the switch is a 3 position toggle switch with center off. I tested the code and still no output for LED 2 and motor 2.
If I go back to my original code without a timer, both outputs work fine when toggled. This allows me to independently turn on each set (LED and motor) with the toggle. They will stay on until turn the toggle is off.
I need LED 2 and motor 2 on a timer that will run for 1 minute on then 1 minute off. This cycle will run for 2 hrs total duration of time and then needs to shut off completely.
Here is the original code that works with independent toggling:
/*
FUNCTION
Button 1 will operate motor until shut off with button
Button 2 will operate motor on a timer with end timer after X amount of time has elapsed
Buttons consist of (1) 3 position toggle with center off.
CIRCUIT
10K resistor between pins 2 and GND
10k resistor between pins 3 and GND
Button 1 and 2 attached to +5v
Button 1 = pin 2
Button 2 = pin 3
LED 1 = pin 10
LED 2 = pin 11
Motor 1 = pin 12
Motor 2 = pin 13
TIMER
Motor 2 and LED 2 = 1 minute on then 1 minute off
Timer needs to end and shut the loop down after 2 hrs of run time.
*/
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 2; // the number of the push button pin
const int buttonPin2 = 3; // the number of the push button pin
const int ledPin1 = 10; // the number of the LED pin
const int ledPin2 = 11; // the number of the LED pin
const int motorPin1 = 12; // the number of the MOTOR pin
const int motorPin2 = 13; // the number of the MOTOR pin
const long interval = 1000; // interval at which to LED 2 and Motor 2 activate (milliseconds)
// variables will change:
int buttonState1 = LOW; // variable for reading the push button status
int buttonState2 = LOW; // variable for reading the push button status
void setup() {
// initialize the pins as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
// initialize the push button pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop() {
// read the state of the push button value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
// check if the push button is pressed.
// if it is, the button State is HIGH:
if (buttonState1 == HIGH) {
// turn LED 1 + motor 1 on:
digitalWrite(ledPin1, HIGH);
digitalWrite(motorPin1, HIGH);
} else {
// turn LED 1 and motor 1 off:
digitalWrite(ledPin1, LOW);
digitalWrite(motorPin1, LOW);
}
// Motor 2 output + LED 2 on a timer with delay and end after certain time.
// Motor 2 + LED 2 delay should be 1 minute on then 1 minute off
// Total time to run should be 2hrs then shut off loop
if (buttonState2 == HIGH) {
// turn LED 2 + motor 2 on:
digitalWrite(ledPin2, HIGH);
digitalWrite(motorPin2, HIGH);
} else {
// turn LED 2 and motor 2 off:
digitalWrite(ledPin2, LOW);
digitalWrite(motorPin2, LOW);
}
}