Hello everyone Im new to Arduino and the language used and would like to learn more,
That being said Id love to be able controll my setup of gate motor and can't find (lack of knowlage) something to interupt the loop and return to openGate
here is the code
const int buttonPin = 10; // Give pin 10 a constant name
const int openGate = 13; // Give pin 13 a constant name
const int closeGate = 12; // Give pin 12 a constant name
int buttonState = 0; // Give varable button a state value
void setup() {
pinMode(openGate, OUTPUT); // Make pin 13 an output
pinMode(closeGate, OUTPUT); // Make pin 12 an output
pinMode(buttonPin, INPUT); // Make pin 10 an input
}
void loop() {
buttonState = digitalRead(buttonPin); // Read button value or state
if (buttonState == HIGH){
digitalWrite(openGate, HIGH);
delay(2000);
digitalWrite(closeGate, HIGH); // Ill like to have it on button press to return to (openGate, HIGH)
delay(2000);
}
else{
digitalWrite(openGate, LOW);
digitalWrite(closeGate, LOW);
}
}
You've got four seconds of delays in there - if you want to respond to a button press, get rid of them.
Look at the blink without delay example for clues.
Please remember to use code tags when posting code.
yes 10kilo ohm to ground if depressed will make pin 10 low
thanx for asking luckily I'm testing everything on https://circuits.io before anything will be connected to my recently received Arduino
okay done away with delay function how do I now interrupt the unsigned long currentMillis = millis(); function could you point me in the right direction please?
The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing. It may help with understanding the technique.
Note how everything is done a little at a time so that there are opportunities to do other things.
If I understand what you want the first button press opens the gate and the next closes it.
If that is correct you need to keep track of whether the gate is open or closed. When the button is pressed open it if it's closed or else close it.
I added a boolean variable gateIsOpen to your code to show you what I mean, but I'm not sure how your setup works so you should change it as necessary.
const int buttonPin = 10; // Give pin 10 a constant name
const int openGate = 13; // Give pin 13 a constant name
const int closeGate = 12; // Give pin 12 a constant name
int buttonState = 0; // Give varable button a state value
boolean gateIsOpen = false; // true => gate is open
void setup() {
pinMode(openGate, OUTPUT); // Make pin 13 an output
pinMode(closeGate, OUTPUT); // Make pin 12 an output
pinMode(buttonPin, INPUT); // Make pin 10 an input
}
void loop() {
buttonState = digitalRead(buttonPin); // Read button value or state
if (buttonState == HIGH){
// if gate is open
if( gateIsOpen )
{
// close it
digitalWrite(closeGate, HIGH);
} else {
// open it
digitalWrite(openGate, HIGH);
} // else
// set flag to reflect current state
gateIsOpen = !gateIsOpen;
}
else{
digitalWrite(openGate, LOW);
digitalWrite(closeGate, LOW);
}
}
Not exactly what id like is if the button is pressed the gate fully opens stays open for x amount time then closes but upon closing either the button or if installed I/R should interrupt the process by restarting the loop over again if not it should return to zero and listen to button pressed.
at the end of the day Id like to use my Arduino to open both my gates using the same method using different pins (and sensors ) of coarse
I must add the motors is normal dc12volt not stepper motors and the closing and opening times can be set for exactly the time it takes to open and close I can always add a normally closed switch in line with the motors if the time is too high the switches will break the connection stopping the motors instantly both is pin and crown gear boxed type and very slow
HannesWallace:
Seems like doesn't matter what I do ill have to have more inputs (switches) to declare current state?
No, I don't think you need more switches. You have a switch that's activated when fully open and another when fully closed. That defines three states that you can measure in the world. (Actually there's a fourth: the error state when both switches are activated at the same time.)
The rest of it is just internal memory inside the Arduino. If the gate was open and we started driving the motor to the 'closed' position but it hasn't yet activated the closed switch, then we are in the CLOSING state. If you have been in the CLOSING state more than 90 seconds and it hasn't yet got to CLOSED then there's probably some error, so stop.
Adding a 5-minute timeout on the OPEN state is also relatively easy - just more items to remember in software. Remember when it reached OPEN, then if it's still in OPEN after 300000 milliseconds, start closing.
It looks like perhaps you built that circuit on circuits.io by using the 'breadboard' view. Please don't do that. You are just making it difficult for yourself. Use the schematic view. Then you can make the 4 transistors look like a conventional H-bridge. For me looking at that circuit, I can't tell if you have actually wired an H-bridge or something else.
Oh - I thought that there were two buttons: one to open the gate and one to close it.
Still, you can see how the "state machine" pattern works, I hope. If you want to use bump sensors rather than timings, then yes you will need two bump sensors (buttons) as well as the 'gate open!' button itself.
Robin2:
That would make life a lot simpler. Is there any reason not to have two buttons?
Basically - that's not what he wants his machine to do. He wants a button that opens the gate, hold it open for a couple of seconds, then closes it again.
It's really not any more complicated than the two-button thing.
Okay I've managed to compile code that checks out in compiler but when uploaded to board does not work also would like to add a interrupt button when the loop reaches the closing state bypasses and goes to opening state any help will be greatly appreciated
const byte remote = 2; // Give pin 2 a constant name
const byte openRl = 13; // Give pin 13 a constant name
const byte closeRl = 12; // Give pin 12 a constant name
const byte closedBtn = 11; // Give pin 11 a constant name
const byte openedBtn = 10; // Give pin 10 a constant name
const byte errLed = 9; // Give pin 9 a constant name
int remoteState = LOW;
int openRlState = LOW;
int closeRlState = LOW;
int closedBtnState = LOW;
int openedBtnState = LOW;
int errLedState = LOW;
enum State { // Call state function
CLOSED, OPENING, OPEN, CLOSING // Give program possible states
} state = CLOSED; // Set the gate to a closed state
unsigned long tslr = 0; // Call timer function
long interval_opening = 10000; // Create timer for opening state
long interval_closing = 10000; // Create timer for closing state
long interval_pausing = 20000; // Create timer for pausing/opened state
void setup() {
pinMode(remote, INPUT); // Make pin 2 an input
pinMode(openRl, OUTPUT); // Make pin 13 an output
pinMode(closeRl, OUTPUT); // Make pin 12 an output
pinMode(closedBtn, INPUT); // Make pin 11 an input
pinMode(openedBtn, INPUT); // Make pin 10 an input
pinMode(errLed, OUTPUT); // Make pin 9 an output
Serial.begin(9600); // Begin serial monitor for fault finding
delay(150); // Delay program so monitor can connect
boolean errLed = LOW;
Serial.println("Program Loaded!");
}
void loop() {
remoteState = digitalRead(remote); // Tell loop how to get remote state
openedBtnState = digitalRead(openedBtn); // Tell loop how to get opened button state
closedBtnState = digitalRead(closedBtn); // Tell loop how to get closed button state
errLedState = digitalRead(errLed); // Tell loop how to get error led state
closeRlState = digitalRead(closeRl); // Tell loop how to get close relay state
openRlState = digitalRead(openRl); // Tell loop how to get open relay state
int prevRemoteState = remoteState; // Tell loop where to get previos remote state
switch (state) { // Tell loop what the enum state is
case CLOSED: // Switch state to closed
if (remoteState == HIGH) { // Look for remote state change
tslr = millis(); // Get time form timer
while((millis() - tslr) < interval_opening); // Tell loop to use open interval
digitalWrite(openRl, HIGH); // Tell open relay to close
Serial.println("OPENING GATE NOW!"); // Tell serial what to display
state = OPENING; // Switch state of gate to opening
}
else if(remoteState == LOW){
digitalWrite(openRl, LOW);
Serial.println("WAITING FOR INPUT FROM REMOTE");
return;
}
else if (openedBtn == HIGH) { // Tell loop to pause while opened
tslr = millis(); // Get time form timer
while((millis() - tslr) < interval_pausing); // Tell loop to use pauseing interval
digitalWrite(openRl, LOW); // Tell open relay to open
Serial.println("GATE OPENED!"); // Tell serial what to display
state = OPEN; // Switch state of gate
}
else if(openedBtn == LOW) {
digitalWrite(errLed, HIGH); // Light up error led
Serial.println("ERROR OPEN BUTTON NOT HIGH"); // Tell serial what to display
errLed == HIGH;
while(1){} // Stop loop here and wait for reset
}
break;
case OPEN: // Tell loop what the enum state is
if (prevRemoteState = LOW && remoteState == HIGH) { // Look for remote state change
tslr = millis(); // Get time form timer
while((millis() - tslr) < interval_pausing); // Tell loop to use pauseing interval
digitalWrite(closeRl, HIGH); // Tell open relay to close
Serial.println("GATE OPENED WAITING FOR PAUSE TO EXPIRE!"); // Tell serial what to display
state = CLOSING; // Switch state of gate
}
break;
case CLOSING:
if (prevRemoteState = HIGH && remoteState == LOW) { // Tell loop what the enum state is
tslr = millis(); // Get time form timer
digitalWrite(closeRl, HIGH); // Tell close relay to close
digitalWrite(openRl, LOW); // Tel open relay to open
Serial.println("GATE CLOSING NOW"); // Tell serial what to display
state = OPENING; // Switch state of gate
}
else { // Tell loop to pause while opened
tslr = millis(); // Get time form timer
while((millis() - tslr) < interval_pausing); // Tell loop to use pauseing interval
digitalWrite(openRl, LOW); // Tell open relay to open
Serial.println("GATE OPENED WAITING FOR PAUSETO EXPIRE!"); // Tell serial what to display
state = OPEN;
}
break;
}
}
It seems to me unlikely that the processor isn't running, so if it is, it can be said to be working.
If so, it should be generating debug messages.
But you haven't posted any of those.