Hello so here is what i want to do :
-
Motor should be off for 100 seconds.
-
17 Seconds before Motor Turns on start something ( Code under here is not exactly what but it doesnt matter at this point.
-
After the preroll stuff is done check the Ir sensor.
-
If the ir sensor is false. make a Led high for 5 seconds and turn it off and then wait 15 seconds before trying again.
-
if the ir sensor after lets say 3 attempts is true then turn on the motor for 3 seconds and restart the process.
hope i explained myself good enough, been grinding my head over this one. Thanks for help!
const unsigned int Ontime = 3000; // How long the motor should stay on.
const unsigned long Offtime = 100000; // How Long the motor should stay off.
const unsigned long preroll = 17000; // How long time before motor turn on to give a warning sound.
const unsigned long waiting = 15000; // Wait time before it checks the Ir sensor again.
unsigned long previousMillis = 0; // will store last time.
bool motorState = true; //
int irSensorState = false;
void setup() {
pinMode(motorPin, OUTPUT);
Serial.begin(9600); //Start serial communication boud rate at 9600
pinMode(irSensor, INPUT); //Pin 5 as signal input
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}
void loop() {
if(digitalRead(irSensor)==LOW) {
irSensorState = false;
// Collision detected.
}
else {
irSensorState = true;
// No collision detected.
}
if (Motorstate == false)
{
if (currentMillis - previousMillis >= (Offtime - preroll)) // start this 17 seconds before Ontime.
digitalWrite(Ledpin, HIGH);
}
// maybe here i want a code that check if the Ir sensor is either true or false,
//if its false i want to check it again in 15 seconds (( const unsigned long waiting = 15000;)
// and then do it every 15 seconds untill its true, and then Turn on the motor pin.
if (currentMillis - previousMillis >= Offtime)
{
previousMillis = currentMillis;
motorState = !motorState;
digitalWrite(motorPin, motorState);
}
}
else
{
if (currentMillis - previousMillis >= Ontime)
{
previousMillis = currentMillis;
motorState = !motorState;
digitalWrite(motorPin, motorState);
signalSent = false; // Set it back to false again to restart the proces
}
}
}