ive got a question for the Much more educated people here and was wondering if it would be possible to do?
if (statement == true){ // IF CONDITION IS SET
digitalWrite(xxx, HIGH); // TURN ON XXX
While (xxx == HIGH > xAmountOfTime); // WHILE XXX IS ON FOR HIGHER THAN SET TIME
digitalWrite(xxx, LOW); // TURN OFF XXX
digitalWrite(yyy, HIGH); // TURN ON YYY
}
That code you posted has a number of serious issues. In fact, I'd say that it's pretty much composed entirely of issues without any piece of the code doing what you want it to.
C is case sensitive - there is no such thing as a While loop - you're looking for a while loop.
digitalWrite(xxx, HIGH);
While (xxx == HIGH > xAmountOfTime);
Here, it looks like xxx is a pin number.
But then on the next line you compare the pin number to the constant HIGH. This does not read the pin - you need to use digitalRead() to check the value on the pin.
Also on that same line, you then take the result of xxx == HIGH, and compare it to xAmountOfTime. However, you're not actually doing any time-keeping here. The result of xxx==HIGH (or any boolean expression) is either true (1) or false (0) - it is not the length of time the condition has been true or false for.
You also appear to misunderstand what a while loop does. In a while loop, it checks the condition at the top. If it is false, the while loop exits, otherwise it runs the code inside the while loop, then checks the condition again.
Your use case is probably appropriate for a blink-without-delay type solution - but you're going to need to review the basics of the C programming language and Arduino libraries.
No unfortunately its not the only code I don't like wile either but how else do I go about stopping the current action in progress and do something else instead of that progress was to slow?
I've looked at it in numerous ways and simply can not figure it out >:(
Hi DrAzzy;
Yes you're 100% correct ill give an better example
void loop() {
switch (state) {
case CLOSED:
if (buttonA == HIGH) {
digitalWrite(motor, HIGH);
Serial.println("OPENING");
state = OPENING:
while (motor = HIGH > 6000);
digitalWrite(errorLed, HIGH);
Serial.println("ERROR WHILE OPENING");
while (true) {}
}else {
Serial.println("WAITING FOR INPUT");
}
break;
case OPENED:
if (buttonB == HIGH) {
digitalWrite(motor, LOW);
Serial.println("OPENED FULLY");
} else {
Serial.println("ERROR NOT FULLY OPEN");
}
}
}
What do you expect this statement to do? Assigning the value HIGH to a pin number doesn't make sense. Comparing the result of that assignment (which is the value assigned) to 6000 kind of sort of makes sense. But, doing that in a while statement does not, because if the while statement's clause is false, nothing happens. But, if the clause is true, the code spins forever.
What I suspect that you want to do is to run the motor for not more than 6 seconds. Or, maybe it is run the motor for more than 6 seconds, though that seems unlikely.
If you want to run the motor for not more than 6 seconds, then you need a while statement that is based on the time you turned the motor on.
digitalWrite(motor, HIGH);
unsigned long startedMotor = millis();
while(millis() - startedMotor < 6000)
{
// Determine whether the condition needed to stop the motor
// (it hit a limit switch or it drew too much current or whatever)
// is true. If it is, break out of the while loop
}
I still cant figure it out It either waits for the set time and then opens the gate or it does nothing the way I construct it.
I'm 100% sure this argument is busy kicking my ass so I'm in dire need of your assistance please.
Could you put a little bit more detail into your code ?
So I can be set on a course of success?
Hello Hannes, what I get is :
You have a motor that opens a gate.
You want the motor to run and open the gate until a limit switch is pressed.
If the opening time exceeds 6 seconds, you want to stop the motor and print an error message.
Is that correct?
I still cant figure it out It either waits for the set time and then opens the gate or it does nothing the way I construct it.
I'm 100% sure this argument is busy kicking my ass so I'm in dire need of your assistance please.
Could you put a little bit more detail into your code ?
So I can be set on a course of success?
How about posting your updated sketch so we might be able to see where you go wrong.
Your arduino executes the loop() function over and over, hundreds of thousands of times a second. To make anything happen, your loop() has to answer the question "What do I need to do right now?".
Almost always, the answer is "nothing". But sometimes a condition becomes true (or false), or something changes, and the loop needs to respond to that.
A common thing is timing. your loop needs to do something when some time has elapsed. To aid with this, the arduino provides the millis() function which gives you the number of milliseconds since the arduino was last reset.
To time anything, when a thing happens, the loop will take care of the thing (setting a pin high, whatever) and it puts the current millis() value in a variable of type uint32_t named "start of timer" or whatever. This variable needs to be declared outside the loop (it needs to have "static scope"). Subsequently, each time it runs loop will subtract the c"start of timer" from current millis() to get "nuber of millis since whatever". When that exceeds the timeout, well, just like any other condition that the loop() has to deal with - that timeout has to be dealt with.
In terms of your extremely wrong code,
uint32_t startOfXxxTimer;
void loop() {
if (statement == true) { // IF CONDITION IS SET
digitalWrite(xxx, HIGH); // TURN ON XXX
startOfXxxTimer = millis();
}
else if (digitalRead(xxx) == HIGH) { // IF WE ARE CURRENTLY TIMING X
if(millis() - startOfXxxTimer >= xAmountOfTime) {
digitalWrite(xxx, LOW); // TURN OFF XXX
digitalWrite(yyy, HIGH); // TURN ON YYY
}
}
}
Okay tried it using the suggestions raised still not working here is all of my code
const byte remote = 12; // Give pin 12 a constant name
const byte openRl = 9; // Give pin 9 a constant name
const byte closeRl = 8; // Give pin 8 a constant name
const byte closedBtn = 10; // Give pin 10 a constant name
const byte openedBtn = 11; // Give pin 11 a constant name
const byte errLed = 13; // Give pin 13 a constant name
const byte ir = A0; // Give pin A0 a constant name
int remoteState = remote; // Tell controler where to get remote state
int openRlState = openRl; // Tell controler where to get open relay state
int closeRlState = closeRl; // Tell controler where to get close relay state
int closedBtnState = closedBtn; // Tell controler where to get closed button state
int openedBtnState = openedBtn; // Tell controler where to get opened button state
int errLedState = errLed; // Tell controler where to get error led state
int irState = ir; // Tell controler where to get infra red state
enum State { // Call state function
CLOSED, OPENING, OPEN, CLOSING // Define possible states
}
state = CLOSED; // Define current state
unsigned long tslr = 0; // Call timer function and give it a name
long interval_pausing = 10000; // Define time of pausing interval (time set is only for testing purposes)
long interval_opening = 10000; // Define time of opening interval (time set is only for testing purposes)
long interval_closing = 1000; // Define time of closing interval (time set is only for testing purposes)
void setup() {
pinMode(remote, INPUT_PULLUP); // Make remote pin an input
pinMode(openRl, OUTPUT); // Make open relay pin an output
pinMode(closeRl, OUTPUT); // Make close relay pin an output
pinMode(closedBtn, INPUT_PULLUP); // Make closed button pin an input
pinMode(openedBtn, INPUT_PULLUP); // Make opened button pin an input
pinMode(errLed, OUTPUT); // Make error led pin an output
pinMode(ir, INPUT_PULLUP); // Make infra red pin an input
Serial.begin(9600); // Begin serial monitor (for now for fault finding only)
delay(150); // Delay for serial to start
Serial.println("ALL VARABLES LOADED!"); // Print to serial that till here program is fine
}
void loop() {
remoteState = digitalRead(remote); // Tell loop how to get previous remote state
openedBtnState = digitalRead(openedBtn); // Tell loop how to get previous opened button state
closedBtnState = digitalRead(closedBtn); // Tell loop how to get previous closed button state
closeRlState = digitalRead(closeRl); // Tell loop how to get previous close relay state
openRlState = digitalRead(openRl); // Tell loop how to get previous open relay state
errLedState = digitalRead(errLed); // Tell loop how to get previous error led state
irState = digitalRead(ir); // Tell loop how to get previous infra red state
int prevRemoteState = remoteState; // Tell loop how to get previous remote state
int prevOpenedBtnState = openedBtnState; // Tell loop how to get previous opened button state
int prevClosedBtnState = closedBtnState; // Tell loop how to get previous closed button state
int prevIrState = irState; // Tell loop how to get previous infra red state
tslr = millis(); // Tell loop where to get time since last reset
switch (state) { // Call state switch function
case CLOSED: // Give the gate a curent state
if (remoteState == LOW) { // If remote is pressed using low because of pullup resitor
digitalWrite(openRl, HIGH); // Then turn on the open relay so gate can open
Serial.println("OPENING GATE NOW !!!"); // And say the gate is opening on serial monitor
state = OPENING; // Change the state of the gate
}
break; // Break from closed state
case OPENING: // State what case will be handled now
if (openedBtnState == LOW) { // if opened button is pressed
digitalWrite(openRl, LOW); // Switch off the open relay
Serial.println("GATE OPENED !!!"); // Say that the gate is open on serial monitor
state = OPEN; // Change state of the gate to open
tslr = millis();
}
else if (openedBtnState == HIGH) { // Else if opened button is not pressed
if(millis() - tslr >= interval_opening) { // if opening interval (10 second)is higher than millis minus time since last reset (not working)
digitalWrite(errLed, HIGH); // Light up the error led
Serial.println(" ERROR WHILE OPENING"); // And print to serial error while opening
while (1) {} // Stop working when error led is lit
}
}
break; // Break from opening state
case OPEN: // State what case will be handled now
delay(150); // Delay between serial prints so I know the states has been changed
Serial.println("WAITING FOR CAR!!!"); // Print to serial waiting for car ( here i'd like to implement the ir so the loop return to top if trigerded)
while(millis() - tslr < interval_pausing); // while millis minus time since last reset is
Serial.println("GATE CLOSING !!!"); // Print to serial that gate is closing
digitalWrite(openRl, LOW); // Make sure the open relay is off ( found in some cases the relay switches back on with close relay not wile running this code though
digitalWrite(closeRl, HIGH); // Turn on close relay to close gate
state = CLOSING; // Change state to closing
break; // Break from open state
case CLOSING: // State what case will be handled now ( will also like to add the error led function in here after its working on top
if (closedBtnState == LOW) { // if the closed button is pressed
digitalWrite(openRl, LOW); // Turn off the open relay
digitalWrite(closeRl, LOW); // Turn off the closing relay
Serial.println("GATE CLOSED !!!"); // Print to serial that gate is closed
state = CLOSED; // Change state of gate to closed
}
}
}
int prevRemoteState = remoteState; // Tell loop how to get previous remote state
int prevOpenedBtnState = openedBtnState; // Tell loop how to get previous opened button state
int prevClosedBtnState = closedBtnState; // Tell loop how to get previous closed button state
int prevIrState = irState;
If you want those values to persist, you need to qualify the variables as "static".
Just saying something is "still not working" helps no-one.
Okay thanx AWOL (learning ) so I would have to change the integer "int" to "static" in void loop if I understand correctly?
and on your comment of "just saying something is still not working" I do agree but I've tried it all ways possible and according to PaulMurrayCbr that's the way so I'm actually asking for help as I'm stuck and even tried looking it up in study material but cant wrap my head around it
You may have tried "all ways possible", but you didn't share you expectations and how your observations failed to meet them.
Had you done that, it might've been easier to offer better advice - trawling through someone else's code with no idea what it did and what it was supposed to do is no-one's idea of fun.
else if (openedBtnState == HIGH) { // Else if opened button is not pressed
if(millis() - tslr >= interval_opening) { // if opening interval (10 second)is higher than millis minus time since last reset (not working)
digitalWrite(errLed, HIGH); // Light up the error led
Serial.println(" ERROR WHILE OPENING"); // And print to serial error while opening
while (1) {} // Stop working when error led is lit
What this needs to do is if the opened button state goes from low to high (depressed to pressed at this stage ) and its not doing so in set time by means of interval_opening it should light up the led and print to serial and then stop the loop while keeping it an that state(led high and one line on serial.
Ive tried to change the >= to < and then gets the led lit up and the serial print as it should but then from the press of the remote button so in my limited coding skills this should work as it is but even after 20 seconds no error led is lit
sorry for the stupid question can see why you didint awnser in the first place
it should be
const int prevRemoteState = remoteState; // Tell loop how to get previous remote state
const int prevOpenedBtnState = openedBtnState; // Tell loop how to get previous opened button state
const int prevClosedBtnState = closedBtnState; // Tell loop how to get previous closed button state
const int prevIrState = irState; // Tell loop how to get previous infra red state
HannesWallace:
sorry for the stupid question can see why you didint awnser in the first place
it should be
const int prevRemoteState = remoteState; // Tell loop how to get previous remote state
const int prevOpenedBtnState = openedBtnState; // Tell loop how to get previous opened button state
const int prevClosedBtnState = closedBtnState; // Tell loop how to get previous closed button state
const int prevIrState = irState; // Tell loop how to get previous infra red state
Why? You don't even use those as far as I can see; I would say "get rid of them".