So what i tried is this:
1: Create a "long" for "last known event" (last)
2: created a "long" for the delay (interval) although im not sure why i cant just refer to "2000" in the code where required, but that is how the blink tutorial did it so fine.
3: in the line following arguments, (for 000, 001, 010, 011, 100, 110) i have asked it to assign "last" with the current time. I HOPE this will update the "last" value every state change.
4: finally for argument 111:
if now - last < interval { carry on!}
if now - last > interval {all stop!}
(I would like this to declare the end to the code, but it isn't necessary, as once it has stopped, it wont be changing states on the sensors.
i have enclosed this last section between two lines of asterixes ************************** near the bottom of the sketch.
in practice as soon as the argument 111 is reached, it stops.
i want it to carry on for 2000 ms, and then if the argument is not changed within the 2000 ms, THEN stop.
if anyone could point out what ive missed it would be greatly appreciated.
Thanks all, sorry if im being thick.
#include <AFMotor.h>
AF_DCMotor motorL(3, MOTOR12_1KHZ);
AF_DCMotor motorR(2, MOTOR12_1KHZ);
// These constants won't change:
const int LeftPR = A1; // pin that the sensor is attached to
const int CentrePR = A2; // pin that the sensor is attached to
const int RightPR = A3; // pin that the sensor is attached to
const int IndicatorL = 10; // pin that the LED is attached to
const int IndicatorC = 9; // pin that the LED is attached to
const int IndicatorR = 19; // pin that the LED is attached to
const int button = 18;
// finish line timing
long last= 0;
long interval = 2000; // delay time
// variables:
int LeftValue = 0; // the sensor value
int LeftMin = 1023; // minimum sensor value
int LeftMax = 0; // maximum sensor value
int CentreValue = 0; // the sensor value
int CentreMin = 1023; // minimum sensor value
int CentreMax = 0; // maximum sensor value
int RightValue = 0; // the sensor value
int RightMin = 1023; // minimum sensor value
int RightMax = 0; // maximum sensor value
void setup() {
// Serial monitor
Serial.begin(9600); // set up Serial library at 9600 bps
//Motors
//set the speed to 200 of 255 of "motorL".
//Note that 0 is stop, 255 is full speed:
motorL.setSpeed(200);
// set the speed to 200 of 255 of "motorR":
motorR.setSpeed(200);
//Calibraton
// turn on LEDs to signal the start of the calibration period:
pinMode(IndicatorL,OUTPUT);
pinMode(IndicatorC,OUTPUT);
pinMode(IndicatorR,OUTPUT);
digitalWrite(IndicatorL, HIGH);
digitalWrite(IndicatorC, HIGH);
digitalWrite(IndicatorR, HIGH);
// calibrate during the first 5 seconds
{
while (millis() < 5000) {
//Spin on the spot
motorL.run(FORWARD); // turn it on going forward
motorR.run(BACKWARD); // motor 2 goes forward as well
//Calibrate
LeftValue = analogRead(LeftPR);
// record the maximum sensor value
if (LeftValue > LeftMax) {
LeftMax = LeftValue;
}
// record the minimum sensor value
if (LeftValue < LeftMin) {
LeftMin = LeftValue;
}
CentreValue = analogRead(CentrePR);
// record the maximum sensor value
if (CentreValue > CentreMax) {
CentreMax = CentreValue;
}
// record the minimum sensor value
if (CentreValue < CentreMin) {
CentreMin = CentreValue;
}
RightValue = analogRead(RightPR);
// record the maximum sensor value
if (RightValue > RightMax) {
RightMax = RightValue;
}
// record the minimum sensor value
if (RightValue < RightMin) {
RightMin = RightValue;
}
}
motorL.run(RELEASE); // turn it on going forward
motorR.run(RELEASE); // motor 2 goes forward as well
// signal the end of the calibration period
digitalWrite(IndicatorL, LOW);
digitalWrite(IndicatorC, LOW);
digitalWrite(IndicatorR, LOW);
delay(1000);
digitalWrite(IndicatorC, HIGH);
delay(100);
digitalWrite(IndicatorC, LOW);
delay(100);
digitalWrite(IndicatorC, HIGH);
delay(100);
digitalWrite(IndicatorC, LOW);
delay(100);
digitalWrite(IndicatorC, HIGH);
delay(100);
digitalWrite(IndicatorL, LOW);
digitalWrite(IndicatorC, LOW);
digitalWrite(IndicatorR, LOW);
}
//hold
while (digitalRead (button) == HIGH){
// stops script. Its waiting for a button press (LOW on "button")
}
}
void loop() {
{
//LEFT SENSOR
LeftValue = analogRead(LeftPR);
// apply the calibration to the sensor reading
LeftValue = map(LeftValue, LeftMin, LeftMax, 0, 100);
LeftValue = constrain(LeftValue, 0, 200);
// in case the sensor value is outside the range seen during calibration
if (LeftValue > 100) {
digitalWrite (IndicatorL, HIGH);
}
if (LeftValue < 100) {
digitalWrite (IndicatorL, LOW);
}
//CENTRE SENSOR
CentreValue = analogRead(CentrePR);
// apply the calibration to the sensor reading
CentreValue = map(CentreValue, CentreMin, CentreMax, 0, 100);
CentreValue = constrain(CentreValue, 0, 200);
// in case the sensor value is outside the range seen during calibration
if (CentreValue > 100) {
digitalWrite (IndicatorC, HIGH);
}
if (CentreValue < 100) {
digitalWrite (IndicatorC, LOW);
}
// RIGHT SENSOR
RightValue = analogRead(RightPR);
// apply the calibration to the sensor reading
RightValue = map(RightValue, RightMin, RightMax, 0, 100);
RightValue = constrain(RightValue, 0, 200);
// in case the sensor value is outside the range seen during calibration
if (RightValue > 100) {
digitalWrite (IndicatorR, HIGH);
}
if (RightValue < 100) {
digitalWrite (IndicatorR, LOW);
}
}
// Line following arguments
// Forward 010, 000 (000 to be set to 5 second time limit once ive worked out how to handle millis)
if ((digitalRead(IndicatorL) == LOW && digitalRead(IndicatorC) == LOW && digitalRead(IndicatorR) == LOW)||
(digitalRead(IndicatorL) == LOW && digitalRead(IndicatorC) == HIGH && digitalRead(IndicatorR) == LOW)){
motorL.run(FORWARD);
motorR.run(FORWARD);
unsigned long last = millis();
}
//Turn right: 001, 011
if ((digitalRead(IndicatorL) == LOW && digitalRead(IndicatorC) == LOW && digitalRead(IndicatorR) == HIGH)||
(digitalRead(IndicatorL) == LOW && digitalRead(IndicatorC) == HIGH && digitalRead(IndicatorR) == HIGH)){
motorL.run(FORWARD);
motorR.run(RELEASE);
unsigned long last = millis();
}
//Turn Left: 100, 110
if ((digitalRead(IndicatorL) == HIGH && digitalRead(IndicatorC) == LOW && digitalRead(IndicatorR) == LOW)||
(digitalRead(IndicatorL) == HIGH && digitalRead(IndicatorC) == HIGH && digitalRead(IndicatorR) == LOW)){
motorL.run(RELEASE);
motorR.run(FORWARD);
unsigned long last = millis();
}
// finish box or cross roads decision **************************************
unsigned long now = millis();
if (digitalRead(IndicatorL) == HIGH && digitalRead(IndicatorC) == HIGH && digitalRead(IndicatorR) == HIGH && (now - last < interval)){
motorL.run(FORWARD);
motorR.run(FORWARD);
}
else if (digitalRead(IndicatorL) == HIGH && digitalRead(IndicatorC) == HIGH && digitalRead(IndicatorR) == HIGH && (now - last > interval)) {
motorL.run(RELEASE);
motorR.run(RELEASE);
}
//***********************************************
Serial.print("Left:");
Serial.print(LeftValue);
Serial.print(" Centre:");
Serial.print(CentreValue);
Serial.print(" Right:");
Serial.print(RightValue);
Serial.println();
}