**HI everyone I am new to programming and I want to try new things with Arduino and this is my problem I have today ..!!!!!! It must start by switching a relay for 1 second then one more relay for 0.5 seconds then one more relay for 3 seconds then the program must stop until a button is pushed . then the sickle must start on top **
TOPIC SPLIT
DO NOT HIJACK / NECRO POST !
Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.
Wire the button from a pin (2, say) to ground, and pinMode() it in setup() thus:
pinMode(2, INPUT_PULLUP); // or whatever number
Then, at the top of loop() put this:
while(digitalRead(2) == HIGH) {}
Then put the relay stuff with delay()s below that.
It will lurk there until the button is pressed (ie,until the pin goes low).
But that's an ugly way of doing things; if your code needs to do stuff other than control those relays, we would need to discuss more "elegant" ways of doing things.
(As a matter of netiquette I'd suggest you lose all the bold in future, which is a bit LIKE SHOUTING.)
I am so bored, here are two solutions: one ugly, one more elegant.
The ugly one is as described in #2 above; the eleganter one is a state machine and includes a delay()-less blink on pin 13 to prove it doesn't block.
Both have the button wired from pin8 to ground. Both were tested with leds (pins 2, 3 & 4) not relays; see comment in code about relay logic and edit if appropriate.
SPOILER ALERT: fully working code below
// https://forum.arduino.cc/index.php?topic=678224
// 18 april
// v1: ugly version
/*
nicolaassteyn asks:
It must start by switching a relay for 1 second then
one more relay for 0.5 seconds then one more relay for
3 seconds then the program must stop until
a button is pushed . then the sickle must start on top
*/
// relay logic is often "active low". I tested this with leds to ground,
// ie active high. For active low, swap the LOW and HIGH below:
// test with led or active high relay:
#define relayON HIGH
#define relayOFF LOW
// active low relay:
//#define relayON LOW
//#define relayOFF HIGH
const byte relay1 = 2;
const byte relay2 = 3;
const byte relay3 = 4;
const byte button = 8;
const int relay1time = 1000;
const int relay2time = 500;
const int relay3time = 3000;
void setup()
{
Serial.begin(9600);
Serial.println("setup()...");
Serial.println("678224 v1 ugly version");
Serial.print("Compiler: ");
Serial.print(__VERSION__);
Serial.print(", Arduino IDE: ");
Serial.println(ARDUINO);
Serial.print("Created: ");
Serial.print(__TIME__);
Serial.print(", ");
Serial.println(__DATE__);
Serial.println(__FILE__);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(button, INPUT_PULLUP);
Serial.println("setup() done");
} //setup
void loop()
{
Serial.print("waiting for button");
while (digitalRead(button) == HIGH) {} //it sits here waiting for a LOW
Serial.print(", relay 1");
digitalWrite(relay1, relayON);
delay(relay1time);
digitalWrite(relay1, relayOFF);
Serial.print("2");
digitalWrite(relay2, relayON);
delay(relay2time);
digitalWrite(relay2, relayOFF);
Serial.println("3");
digitalWrite(relay3, relayON);
delay(relay3time);
digitalWrite(relay3, relayOFF);
} //loop
// https://forum.arduino.cc/index.php?topic=678224
// 18 april
// v2: state machine version with proof-of-life pulse
/*
nicolaassteyn asks:
It must start by switching a relay for 1 second then
one more relay for 0.5 seconds then one more relay for
3 seconds then the program must stop until
a button is pushed . then the sickle must start on top
*/
// relay logic is often "active low". I tested this with leds to ground,
// ie active high. For active low, swap the LOW and HIGH below:
// test with led or active high relay:
#define relayON HIGH
#define relayOFF LOW
// active low relay:
//#define relayON LOW
//#define relayOFF HIGH
const byte relay1 = 2;
const byte relay2 = 3;
const byte relay3 = 4;
const byte button = 8;
const int relay1time = 1000;
const int relay2time = 500;
const int relay3time = 3000;
//states
enum {ST_idle, ST_relay1, ST_relay2, ST_relay3} currentState = ST_idle;
// ala https://www.gammon.com.au/statemachine
unsigned long changedRelayAt;
//pulse
unsigned long previousPulse;
bool pulseState;
const int pulseInterval = 500;
void setup()
{
// initialize serial communication:
Serial.begin(9600);
Serial.println("setup() ... ");
Serial.println("678224 v2 fsm version");
Serial.print("Compiler: ");
Serial.print(__VERSION__);
Serial.print(", Arduino IDE: ");
Serial.println(ARDUINO);
Serial.print("Created: ");
Serial.print(__TIME__);
Serial.print(", ");
Serial.println(__DATE__);
Serial.println(__FILE__);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, pulseState);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(button, INPUT_PULLUP);
Serial.println("setup() done");
Serial.println(" ");
Serial.print("waiting for button");
}
void loop()
{
doPulse();
manageStates();
} //loop
void manageStates()
{
switch (currentState) //ST_idle, ST_relay1, ST_relay2, ST_relay3
{
case ST_idle:
if (digitalRead(button) == LOW)
{
currentState = ST_relay1;
changedRelayAt = millis();
Serial.print(", relay 1");
}
break;
case ST_relay1:
digitalWrite(relay1, relayON);
if (millis() - changedRelayAt >= (unsigned long)relay1time)
{
currentState = ST_relay2;
changedRelayAt = millis();
Serial.print("2");
}
break;
case ST_relay2:
digitalWrite(relay1, relayOFF);
digitalWrite(relay2, relayON);
if (millis() - changedRelayAt >= (unsigned long)relay2time)
{
currentState = ST_relay3;
changedRelayAt = millis();
Serial.println("3");
}
break;
case ST_relay3:
digitalWrite(relay2, relayOFF);
digitalWrite(relay3, relayON);
if (millis() - changedRelayAt >= (unsigned long)relay3time)
{
currentState = ST_idle;
changedRelayAt = millis();
Serial.print("waiting for button");
digitalWrite(relay3, relayOFF);
}
break;
}//switch
}//manageStates
void doPulse()
{
if (millis() - previousPulse >= (unsigned long) pulseInterval)
{
previousPulse = millis();
pulseState = !pulseState;
digitalWrite(LED_BUILTIN, pulseState);
}
}//do pulse
twinkleyan:
I am so bored, here are two solutions:
So Covid-19 will result in certain productivity increases then?
Paul__B:
So Covid-19 will result in certain productivity increases then?
Yeah, a toofer.