Pages: [1]   Go Down
Author Topic: Keep code in the "setup" until button press?  (Read 1111 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My line follower is coming along in leaps and bounds this weekend smiley

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.

i imagine it being along the lines of:

WaitUntil: (Pin18, HIGH);

does this exist?
Logged

Scotland
Offline Offline
Edison Member
*
Karma: 26
Posts: 1325
Have you had your Arduino fix today?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try :

while(digitalRead(18)==LOW)
{
  // Do Nothing
}
Logged

Drew.
http://www.uk-pcb.co.uk - My UK Based PCB Fab & Assembly Company
Design work undertaken
SMD & Thru-Hole assembly

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27114
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, try this:
Code:
while (digitalRead (Pin18) == LOW){
// keep reading until get a HIGH
}

should keep reading Pin18 until it sees a HIGH.

Yeah, like spycatcher said smiley
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4808
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Unless the button grounds pin 18 with pullup on!
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Spot on chaps. smiley
thank you kindly.

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...
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#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();

}

Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27114
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Woo hoo! Made it into the code smiley-cool
Code:
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); 
  }
'Course, could mean something else too smiley-wink
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4808
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay Olly, now take a look at 'else' to go with 'if'.

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27114
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You have a bunch of these
Code:
    // 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
Code:
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?
Code:
  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?
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4808
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Pages: [1]   Go Up
Jump to: