I want to be able to add a LED to be triggered for 5 seconds without blocking any other commands. I do not know how to do non-blocking delays and i need someones help. Thanks in advance
int ledPin = 7;
int Lswitch = 11;
int Stop1 = 12; //bottom
int Stop2 = 13; //top
int motor2pin1 = 2;
int motor2pin2 = 3;
int enB = 5;
int StatE = 0;
int blue = 0;
void setup() {
// put your setup code here, to run once:
pinMode(ledPin, OUTPUT);
pinMode(Lswitch, INPUT_PULLUP);
pinMode(motor2pin1, OUTPUT);
pinMode(motor2pin2, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(Stop1, INPUT_PULLUP);
pinMode(Stop2, INPUT_PULLUP);
Serial.begin(9600);
if (digitalRead(Stop1) == LOW)
{
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
analogWrite(enB, 50);
}
StatE = 1;
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0) { // Checks whether data is comming from the serial port
blue = Serial.read(); // Reads the data from the serial port
}
if ((digitalRead(Stop1) == HIGH) && (StatE == 1))
{
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
analogWrite(enB, 50);
}
if ((digitalRead(Stop2) == HIGH) && (StatE == 2))
{
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
analogWrite(enB, 50);
digitalWrite(ledPin, HIGH);
}
if ((digitalRead(Stop2) == HIGH) && (blue == '1'))
{
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
analogWrite(enB, 50);
StatE = 1;
blue = 0;
}
if ((digitalRead(Stop1) == HIGH) && (blue == '1'))
{
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
analogWrite(enB, 50);
StatE = 2;
blue = 0;
}
if ((digitalRead(Stop1) == LOW) && (digitalRead(Stop2) == LOW) && (blue == '1'))
{
blue = 0;
}
}
Am I right in assuming that you did not write the code in post #1 ?
This is an example from it of testing a button:
if ((digitalRead(Stop1) == LOW))
{
// act on a button press . Button is high side so LOW is pressed and HIGH is not pressed
// here you set the timer and switch the led on.
}
The code will look something like this.
Test it stand alone before integrating it into the rest of your sketch to ensure it all works and you understand it.
const uint8_t myLedPin = X ; // choose a value for X
const uint8_t myButtonPin = Y ; // choose a value for Y
uint32_t myTimer ;
. . .
void setup() {
. . .
pinMode( myLedPin, OUTPUT ) ;
pinMode( myButtonPin, INPUT_PULLUP ) ;
. . .
}
void loop() {
. . .
if( digitalRead( myButtonPin == LOW ) { // button wired so LOW = PRESSED..
myTimer = millis() ;
digitalWrite( myLedPin, HIGH ) ; // LED wired so HIGH = ON
}
if ( digitalRead( myLedPin ) == HIGH && millis() - myTimer > 5000 ) {
digitalWrite( myLedPin, LOW ) ;
}
. . .
}