My line follower is coming along in leaps and bounds this weekend
I would like for it to run the calibration section of the code (which i have put in "setup") and then wait until i press a button for permission to continue.
on button press, i would like it to go to the "loop" and continue as normal.
And its funny you should mention that goforsmoke, as that's how i have it setup, and hadnt noticed initially (and was wondering why it wasnt working)
got my code working well, now just need to adjust my sensor.
Ive done it with no lightsource, and i think it is struggling. I think i need to work out how to add some LEDs to the current setup without too much modification...
#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;
// 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;
  }
 }
 // signal the end of the calibration period
 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
 //Straight:
 if (digitalRead(IndicatorL) == LOW && digitalRead(IndicatorC) == HIGH && digitalRead(IndicatorR) == LOW){
  motorL.run(FORWARD);Â
  motorR.run(FORWARD);Â
 }
 if (digitalRead(IndicatorL) == LOW && digitalRead(IndicatorC) == LOW && digitalRead(IndicatorR) == LOW){
  motorL.run(FORWARD);Â
  motorR.run(FORWARD);Â
 }
 //Cross roads
 if (digitalRead(IndicatorL) == HIGH && digitalRead(IndicatorC) == HIGH && digitalRead(IndicatorR) == HIGH){
  motorL.run(FORWARD);Â
  motorR.run(FORWARD);Â
 }
 //Turn right:
 if (digitalRead(IndicatorL) == LOW && digitalRead(IndicatorC) == LOW && digitalRead(IndicatorR) == HIGH){
  motorL.run(FORWARD);Â
  motorR.run(RELEASE);Â
 }
 //Turn Left
 if (digitalRead(IndicatorL) == HIGH && digitalRead(IndicatorC) == LOW && digitalRead(IndicatorR) == LOW){
  motorL.run(RELEASE); Â
  motorR.run(FORWARD);Â
 }
 Serial.print("Left:");
 Serial.print(LeftValue);
 Serial.print(" Centre:");
 Serial.print(CentreValue);
 Serial.print(" Right:");
 Serial.print(RightValue);
 Serial.println();
}
if (digitalRead(IndicatorL) == LOW && digitalRead(IndicatorC) == LOW && digitalRead(IndicatorR) == LOW){
  motorL.run(FORWARD);Â
  motorR.run(FORWARD);Â
 }
 //Cross roads
 if (digitalRead(IndicatorL) == HIGH && digitalRead(IndicatorC) == HIGH && digitalRead(IndicatorR) == HIGH){
  motorL.run(FORWARD);Â
  motorR.run(FORWARD);Â
 }
  // in case the sensor value is outside the range seen during calibration
  if (LeftValue > 100) {
   digitalWrite (IndicatorL, HIGH);
  }
  if (LeftValue < 100) {
   digitalWrite (IndicatorL, LOW);
  }
I was thinking the 2nd should be < = 100 to cover the condition of 100,
Goforsmoke covers it better with
else // value can only <=100 if > 100 is not met
{
   digitalWrite (IndicatorL, LOW);
}
No need to make the comparison twice in this case. Make it once and then act on it.
It is possible to see the other conditions here?
 if (digitalRead(IndicatorL) == LOW && digitalRead(IndicatorC) == HIGH && digitalRead(IndicatorR) == LOW){
You have these covered
010 (2)
000 (0)
111 (7)
001 (1)
100 (4)
are 3,5,6 not a concern? 011, 101, 110 Or just not able to happen?
Well you can if-else-if-else-if-else till you run out of space,
the idea is that if you have mutually exclusive conditions then why test for them all? If they're not then don't do that.
Sometimes switch-case or another conditional is the way to go... these are things that make arranging logic cleaner, faster executing and less ambiguous.
I wrote code in 78-79 on programmable calculators. One way to know the value of these things is to not have a full set, to learn from what you wish you had and have to make up for. You won't forget soon after that! The TI-56 didn't even have indirect addressing, talk about pain!