I am trying to use the millis()
function to turn on a pin for a specified interval then turn off and turn on a second pin. It is intended to power a relay and offer a visual cue to when the cycle is over. Several of these need to eventually be running, most likely three, so using delay()
won't work. I plan to add additional "button pins" for each input. The LED lights up but gets stuck in the loop and doesn't cycle out. It seemed like a simple program but I can't crack it for some reason.
Is there a much better method to do this? There will be two optical sensors and one pushbutton switch attached to an air driven crushing machine. The two optical sensors will be used only as time indicators with LEDs and the pushbutton will power the relay. Forgive my shortcomings as a programmer, I am learning the programming language still.
const int ledPin = 2; // the number of the LED pin
const int buttonPin = 3; // Button pin number
// Variables will change:
long previousMillis = 0; // will store last time LED was updated
int buttonState = 0; // Checks if button is pushed
int buttonPush = 0; // Toggles between 0 to hold state
long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop()
{
unsigned long currentMillis = millis();
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
buttonPush = 1;
if (buttonPush == 1){
if(currentMillis - previousMillis > interval) {
previousMillis = millis();
digitalWrite(ledPin, HIGH);
}
else {
buttonPush = 0;
}
}
else {
digitalWrite(ledPin, LOW);
}
}
}
Edit: Thank you for looking over the code, you guys are awesome. There were a couple questions regarding clarity of the final application so I will elaborate. I am going to attempt to clean it up. I had a feeling something was wrong with:
if (buttonState == HIGH) {
buttonPush = 1;
but I couldn't put my finger on it.
Eventually the system will function with two heating elements to soften plastic, which I will either add an optical sensor to since it will probably get triggered a few hundred times a day, or just a pushbutton to start a timer so the plastic isn't left in too long. A green LED will be illuminated to indicate the system is on. When the plastic object is placed in the heating element it triggers the optical sensor (or the operator pushes a pushbutton to start the timer) that will then illuminate a red LED to indicate heating. When the specified time interval has expired, the LED will revert back to green to show the cycle is complete. The plastic object is then removed and placed in a pneumatic crushing device with a green system on LED. The operator then pushes a pushbutton which turns on a red indicator LED and a relay to power the solenoid on the pneumatic press for a specified amount of time to allow the plastic to cool and crystallise. When that interval has expired, the system will revert back to illuminating the green LED and will end power to the relay allowing the press to release.
The pneumatic press and the heating system works awesome, I just need better control of the time intervals used. The end result needs to be consistent.
I am going to try all suggestions and will post what ended up working if I reach that point or find an answer that functions.