I am currently making a project where I need the line following robot to follow the line for 10 seconds ONLY when a button is pushed can someone please tell me how to solve this and what code I must use?
You record the time when the button is pushed, and compare the difference between that time and the current time to 10 seconds to end the action.
What code does your line follower robot currently use?
The built-in Arduino example program "blink without delay" shows how to use millis() to time events. File>Examples>02.Digital>...
- Configure a button pin...
int buttonPin = 2; // Pin 2 -- button -- ground
... and get it ready to sense a press...
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // HIGH when not pressed
}
- Configure timers for "when button was pressed" and "for 10 seconds"
unsigned long buttonPressed = 0; // when button was pressed
unsigned long buttonInterval = 10 * 1000; // ten seconds = 10,000 milliseconds
- Monitor button for being pressed... and start a timer
if (!digitalRead(buttonPin)) { // button press should be LOW
buttonPressed = millis(); // start a timer
delay(150); // for debouncing the button press
}
- If time difference between "now" [
millis()
] and "when the button was pressed" is less than "10 seconds" then follow the line.
if (millis() - buttonPressed < buttonInterval) { // if 10 seconds has not expired
// your code to follow the line
}
So what have you done so far?
What you wrote yourself tells what you should know so don't show copied code unless you know it line by line and the answers you get should make more sense, be in words you know kind of help.
When you show nothing, what should I expect? Step up!
its uses code that allows 3 ir sensors to be used and a distance sensor to stop when obstacle in front of the robot
const int rightmotorPin = 11;
const int leftmotorPin = 10;
const int centerirPin = 12;
const int rightirPin = 3;
const int leftirPin = 4;
const int trigPin = 2; //digital pin here
const int echoPin = A5; //analog pin here
int distance;
int time;
int distrange = 30;
int centerVal;
int rightVal;
int leftVal;
int PWMspeed = 90;
void setup() {
// put your setup code here, to run once:
pinMode(rightmotorPin, OUTPUT);
pinMode(leftmotorPin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(centerirPin, INPUT);
pinMode(leftirPin, INPUT);
pinMode(rightirPin, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int centerVal = digitalRead(centerirPin);
int rightVal = digitalRead(rightirPin);
int leftVal = digitalRead(leftirPin);
if((rightVal == 0) && (leftVal == 0)) { //IDEAL CASE
digitalWrite(rightmotorPin, PWMspeed);
digitalWrite(leftmotorPin, PWMspeed);
}
if((rightVal == 1) && (leftVal == 0)) { //TURN RIGHT
digitalWrite(rightmotorPin, LOW);
digitalWrite(leftmotorPin, PWMspeed);
}
if((rightVal == 0) && (leftVal == 1)) { //TURN LEFT
digitalWrite(leftmotorPin, LOW);
digitalWrite(rightmotorPin, PWMspeed);
}
if((rightVal == 1) && (leftVal == 1)) { //STOP
digitalWrite(leftmotorPin, LOW);
digitalWrite(rightmotorPin, LOW);
}
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
int time = pulseIn(echoPin, HIGH); //measure how much time echoPin was HIGH
int distance = time * 0.0343 / 2; //343 speed of soundin air time in microseconds hence 0.0343
if(distance < distrange) { //OBSTACLE AHEAD STOP
digitalWrite(leftmotorPin, LOW);
digitalWrite(rightmotorPin, LOW);
}
Serial.println(distance);
delay(100);
}
I hope this is clear i didnt know how to use code tags my apologies
const int rightmotorPin = 11;
const int leftmotorPin = 10;
const int centerirPin = 12;
const int rightirPin = 3;
const int leftirPin = 4;
const int trigPin = 2; //digital pin here
const int echoPin = A5; //analog pin here
int distance;
int time;
int distrange = 30;
int centerVal;
int rightVal;
int leftVal;
int PWMspeed = 90;
void setup() {
// put your setup code here, to run once:
pinMode(rightmotorPin, OUTPUT);
pinMode(leftmotorPin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(centerirPin, INPUT);
pinMode(leftirPin, INPUT);
pinMode(rightirPin, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int centerVal = digitalRead(centerirPin);
int rightVal = digitalRead(rightirPin);
int leftVal = digitalRead(leftirPin);
if((rightVal == 0) && (leftVal == 0)) { //IDEAL CASE
digitalWrite(rightmotorPin, PWMspeed);
digitalWrite(leftmotorPin, PWMspeed);
}
if((rightVal == 1) && (leftVal == 0)) { //TURN RIGHT
digitalWrite(rightmotorPin, LOW);
digitalWrite(leftmotorPin, PWMspeed);
}
if((rightVal == 0) && (leftVal == 1)) { //TURN LEFT
digitalWrite(leftmotorPin, LOW);
digitalWrite(rightmotorPin, PWMspeed);
}
if((rightVal == 1) && (leftVal == 1)) { //STOP
digitalWrite(leftmotorPin, LOW);
digitalWrite(rightmotorPin, LOW);
}
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
int time = pulseIn(echoPin, HIGH); //measure how much time echoPin was HIGH
int distance = time * 0.0343 / 2; //343 speed of soundin air time in microseconds hence 0.0343
if(distance < distrange) { //OBSTACLE AHEAD STOP
digitalWrite(leftmotorPin, LOW);
digitalWrite(rightmotorPin, LOW);
}
Serial.println(distance);
delay(100);
}
Wow thank you so much I shall check and see if this works
Also, just clarifying this code will work when the button is pressed once like a toggle switch and not held as I need the robot to rotate the tape track fully once (by being on for 10 seconds).
const int rightmotorPin = 11;
const int leftmotorPin = 10;
const int centerirPin = 12;
const int rightirPin = 3;
const int leftirPin = 4;
const int trigPin = 2; //digital pin here
const int echoPin = A5; //analog pin here
int distance;
int time;
int distrange = 30;
int centerVal;
int rightVal;
int leftVal;
int PWMspeed = 90;
void setup() {
// put your setup code here, to run once:
pinMode(rightmotorPin, OUTPUT);
pinMode(leftmotorPin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(centerirPin, INPUT);
pinMode(leftirPin, INPUT);
pinMode(rightirPin, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int centerVal = digitalRead(centerirPin);
int rightVal = digitalRead(rightirPin);
int leftVal = digitalRead(leftirPin);
if((rightVal == 0) && (leftVal == 0)) { //IDEAL CASE
digitalWrite(rightmotorPin, PWMspeed);
digitalWrite(leftmotorPin, PWMspeed);
}
if((rightVal == 1) && (leftVal == 0)) { //TURN RIGHT
digitalWrite(rightmotorPin, LOW);
digitalWrite(leftmotorPin, PWMspeed);
}
if((rightVal == 0) && (leftVal == 1)) { //TURN LEFT
digitalWrite(leftmotorPin, LOW);
digitalWrite(rightmotorPin, PWMspeed);
}
if((rightVal == 1) && (leftVal == 1)) { //STOP
digitalWrite(leftmotorPin, LOW);
digitalWrite(rightmotorPin, LOW);
}
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
int time = pulseIn(echoPin, HIGH); //measure how much time echoPin was HIGH
int distance = time * 0.0343 / 2; //343 speed of soundin air time in microseconds hence 0.0343
if(distance < distrange) { //OBSTACLE AHEAD STOP
digitalWrite(leftmotorPin, LOW);
digitalWrite(rightmotorPin, LOW);
}
Serial.println(distance);
delay(100);
}
I have inserted the pseudo-code from Post #4 in your code from Post #9. See if you can make the changes. Do not worry if everything does not work the first try. You have support here.
const int rightmotorPin = 11;
const int leftmotorPin = 10;
const int centerirPin = 12;
const int rightirPin = 3;
const int leftirPin = 4;
const int trigPin = 2; //digital pin here
const int echoPin = A5; //analog pin here
int distance;
int time;
int distrange = 30;
int centerVal;
int rightVal;
int leftVal;
int PWMspeed = 90;
// Configure a button pin...
// Configure timers "when button was pressed" and "for 10 seconds"
void setup() {
// put your setup code here, to run once:
pinMode(rightmotorPin, OUTPUT);
pinMode(leftmotorPin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(centerirPin, INPUT);
pinMode(leftirPin, INPUT);
pinMode(rightirPin, INPUT);
Serial.begin(9600);
// get button ready to sense a press...
}
void loop() {
// Monitor button for being pressed... and when it is pressed, start a timer
// put your main code here, to run repeatedly:
int centerVal = digitalRead(centerirPin);
int rightVal = digitalRead(rightirPin);
int leftVal = digitalRead(leftirPin);
// the next few if() conditions are your line-following functions (leftVal, rightVal, et c.)
// wrap these conditions in the "is less than 10 seconds" condition
// If time difference between "now" [ millis() ] and "when the button was pressed" is less than "10 seconds" then follow the line.
if ((rightVal == 0) && (leftVal == 0)) { //IDEAL CASE
digitalWrite(rightmotorPin, PWMspeed);
digitalWrite(leftmotorPin, PWMspeed);
}
if ((rightVal == 1) && (leftVal == 0)) { //TURN RIGHT
digitalWrite(rightmotorPin, LOW);
digitalWrite(leftmotorPin, PWMspeed);
}
if ((rightVal == 0) && (leftVal == 1)) { //TURN LEFT
digitalWrite(leftmotorPin, LOW);
digitalWrite(rightmotorPin, PWMspeed);
}
if ((rightVal == 1) && (leftVal == 1)) { //STOP
digitalWrite(leftmotorPin, LOW);
digitalWrite(rightmotorPin, LOW);
}
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
int time = pulseIn(echoPin, HIGH); //measure how much time echoPin was HIGH
int distance = time * 0.0343 / 2; //343 speed of soundin air time in microseconds hence 0.0343
if (distance < distrange) { //OBSTACLE AHEAD STOP
digitalWrite(leftmotorPin, LOW);
digitalWrite(rightmotorPin, LOW);
}
Serial.println(distance);
delay(100);
}
Why is there a center sensor when it is not used?
Why delay(100)? Why delay at all?
Consider that the loop() you have will run in a few microseconds plus 100,000 more.
Most of the time is spent ignoring inputs but 10 times a second we check the button and thesensors. Why not ~1000 times when > 50,000 check and reacts is free?
How close to 10.000 seconds after button press must it stop? If less than a second, you don't need millis() code to catch that.
Faster serial rate empties the print buffer faster, lets you print more over time but beware to never fill the print buffer. I keep my serial monitor set to 115200.
Thank You very much I shall incoorporate this code
I used the delay function for stability so I if it is not needed I shall remove it and I cannot believe I didnt see the center sensor not in use I was working with the current code I shall make changes
if you think about the logic a third (centre) sensor tells you when you are on the line so no correction is needed. it also helps with junctions.
Just because the center sensor is on the line
does not mean the course follows it.
That means more when the line bends and the follower is trying to get back on track.
I've built (or helped with) a few of these over the years - the first, predating microcontrollers, used logic gates!
The problem with a two sensor system is the robot is slow and wastes a LOT of energy continually hunting - which is just ugly.
With a centre sensor and adding in historic data (and appropriate expectations for your line) you can get much smoother progress.
more sensors - more information - better results.
I have seen 5 sensor arrays with swept-back arms, maybe detect turns better?
I dunno if makers 3D printed their own or they were bought online.
Using all sensors wired to pins on the same port, the PINx register always has all of the sensor states as bits that combined answer 2^sensors and/or operations in if()'s. A single switch( sensorStates ) lets every case be coded individually.
I have tried the code and im currently having a problem with the pushbutton im using whenever I press it the windows disconnect sound plays and the board gets disconnected from my computer.
It is obvious that the board is getting a signal when the button is pushed but its not executing my code rather its disconnecting my Arduino board what to do?
Use this reference for wiring a button.
*I think your button is wired such that you are shorting system 5v to system Ground.
Post your current sketch to see how we can help.