Hi all,
VEEEEERY Noob experience level over here; sorry in advance if my code is a trainwreck!
So....
Here's what I'm trying to do.
I've got a motor and I've got an electro mechanical valve (basically just a coil that when energized pulls a piston that allows water to flow out).
The motor has 2 positions, UP and DOWN.
Here's what I'm asking of the Arduino:
- I want to press a momentary button to start the program
- The motor will then lower to its DOWN position and shut off via a closed circuit reading from a roller switch (basically a LOW signal)
- A few seconds later I would like the valve to fire open and then close a few seconds after.
- Then, I would like the motor from step 2 to raise back to the UP positions (this can be a function of time and does not interface with the roller switch).
So, how I have it rigged up now is the motor will start when I hit the button and then it will shut off with the input from the roller switch (this is working). The valve is based on milliseconds timed from when the button is pressed (this is working). I then am trying to raise the motor based on a similar set of code as the millisecond code as the valve (this is NOT working).
Here is the code (the ino file is also attached):
***The following code has been edited 12.7.2016
const byte startButton = 2;
const byte rollerSwitch = 3;
const byte brewMotor = 13;
const byte brewValve = 12;
int currButtonState = LOW; // the current state of the output pin 2
int readButtonState; // the current reading from the input pin 2
int readRollerState; // the current reading from the input pin 3
int prevButtonState = HIGH; // the previous reading from the input pin 2
int prevRollerState = HIGH; // the previous reading from the input pin 3
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
unsigned long buttonPushedMillis; // when button was released
unsigned long brewValveTurnedOnAt; // when brewvalve was turned on
unsigned long brewValveTurnOnDelay = 5000; // wait to turn on brewvalve
unsigned long brewValveTurnOffDelay = 3000; // turn off brewvalve after this time
bool brewValveReady = false; // flag for when button is let go
bool brewValveState = false; // for brewvalve is on or not.
unsigned long brewMotorTurnedOnAt; // when brewmotor was turned on
unsigned long brewMotorTurnOnDelay = 10000; // wait to turn on brewmotor
unsigned long brewMotorTurnOffDelay = 3000; // turn off brewmotor after this time
bool brewMotorReady = false; // flag for when button is let go
bool brewMotorState = false; // for brewmotor is on or not.
void setup()
{
pinMode(startButton, INPUT_PULLUP);
pinMode(brewMotor, OUTPUT);
pinMode(rollerSwitch, INPUT_PULLUP);
pinMode(brewValve, OUTPUT);
digitalWrite(brewMotor, LOW);
}
void loop()
{
readButtonState = digitalRead(startButton);
if (readButtonState == LOW && prevButtonState == LOW && millis() - time > debounce) {
if (currButtonState == HIGH)
currButtonState = LOW;
else
currButtonState = HIGH;
time = millis();
}
digitalWrite(brewMotor, currButtonState);
prevButtonState = readButtonState;
readRollerState = digitalRead(rollerSwitch);
if (readRollerState == LOW && prevRollerState == HIGH && millis() - time > debounce) {
if (currButtonState == LOW)
currButtonState = HIGH;
else
currButtonState = LOW;
time = millis();
}
digitalWrite(brewMotor, currButtonState);
prevRollerState = readRollerState;
unsigned long currentMillis = millis();
if (digitalRead(startButton) == LOW) {
buttonPushedMillis = currentMillis;
brewValveReady = true;
}
if (brewValveReady) {
if ((unsigned long)(currentMillis - buttonPushedMillis) >= brewValveTurnOnDelay) {
digitalWrite(brewValve, HIGH);
brewValveState = true;
brewValveTurnedOnAt = currentMillis;
brewValveReady = false;
}
}
if (brewValveState) {
if ((unsigned long)(currentMillis - brewValveTurnedOnAt) >= brewValveTurnOffDelay) {
brewValveState = false;
digitalWrite(brewValve, LOW);
}
}
digitalWrite(brewMotor, LOW);
if (digitalRead(startButton) == LOW) {
buttonPushedMillis = currentMillis;
brewMotorReady = true;
}
if (brewMotorReady) {
if ((unsigned long)(currentMillis - buttonPushedMillis) >= brewMotorTurnOnDelay) {
digitalWrite(brewMotor, HIGH);
brewMotorState = true;
brewMotorTurnedOnAt = currentMillis;
brewMotorReady = false;
}
}
if (brewMotorState) {
if ((unsigned long)(currentMillis - brewMotorTurnedOnAt) >= brewMotorTurnOffDelay) {
brewMotorState = false;
digitalWrite(brewMotor, LOW);
}
}
}
Any suggestions? Should I lay it out differently?
BUAT_VERSION_1.1.ino (3.17 KB)