Hi Arduino Community, I am new to Ardunio and I am working on a school project that is an automated bottle opener and crusher, I have setup my codes with Delay so the actuator extends then the opener does its things and then it retracts up using Delay in the Void setup area by sequencing different relays. ( I know I should probably look into implementing Millis() function but I did not know how to do it!)
I also have a button setup on my bread board where I want to take priority or have a manual switch to turn on if needed before the loop takes over. ( Right now I have to wait till the Void setup code finishes, then in the loop I have the buttons setup so I can manually crush up and down using the actuator)
What I like to do is either have an override key that my buttons move the actuator up and down before the delay codes work for the opening mechanism OR just have a manual switch so I can assign which code to use at the time, Could you please help?
here are the codes I have:
// constants won't change. They're used here to set pin numbers:
const int button1Pin = 1; // the number of the pushbutton1 pin (Down)
const int button2Pin = 2; // the number of the pushbutton2 pin (Up)
const int RELAY1 = 6; // Actuator Retracting
const int RELAY2 = 7; // Actuator Extending
// variables will change:
int button1State = 0; // variable for reading the pushbutton status
int button2State = 0; // variable for reading the pushbutton status
const int sensorPin = 0; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor
// initialize the pushbutton pin as an input:
// initialize the relay pin as an output:
const int RELAY3 = 4; // Bottle Opener reverse
const int RELAY4 = 5; // Bottle Opener opening
void setup() {
//start serial connection
Serial.begin(9600);
// Initialise the Arduino data pins for OUTPUT
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
digitalWrite(RELAY1, HIGH);
delay(3000);
digitalWrite(RELAY1, LOW);
delay(1000);
digitalWrite(RELAY3, LOW);
delay(1000);
digitalWrite(RELAY1, LOW);
delay(1000);
digitalWrite(RELAY2, HIGH);
delay(1500);
digitalWrite(RELAY2, LOW);
delay(5000);
digitalWrite(RELAY4, HIGH);
delay(5500);
digitalWrite(RELAY4, LOW);
// Wait 5 seconds to retract + 2 + & - 2 -
// Turns OFF Relays 2
delay(5500);
digitalWrite(RELAY3, HIGH);
delay(3000);
digitalWrite(RELAY3, LOW);
delay(3000);
digitalWrite(RELAY1, HIGH); // Turns Relay On
delay(2000); // Wait 45 seconds to retract + 2 + & - 2 -
digitalWrite(RELAY2, LOW);
delay(2000);
digitalWrite(RELAY3, LOW);
delay(2000);
digitalWrite(RELAY4, LOW); // Turns OFF Relays 2
delay(2000);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
//print out the value of the pushbutton
Serial.println(sensorValue);
// read the state of the pushbutton values:
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
// check if the pushbutton1 is pressed.
// if it is, the buttonState is HIGH:
// we also ensure tha the other button is not pushed to avoid conflict
if (button1State == HIGH && button2State == LOW) {
// turn relay1 on:
digitalWrite(RELAY1, HIGH);
}
// When we let go of the button, turn off the relay
else if (digitalRead(RELAY1) == HIGH) {
// turn relay1 off:
digitalWrite(RELAY1, LOW);
}
// repeat the same procedure for the second pushbutton
if (button1State == LOW && button2State == HIGH) {
// turn relay2 on:
digitalWrite(RELAY2, HIGH);
}
// When we let go of the button, turn off the relay
else if (digitalRead(RELAY2) == HIGH) {
// turn relay2 off:
digitalWrite(RELAY2, LOW);
}
}