Hello all,
i use the Lego Roverbot with Line Tracking Sensor for Arduino
This Code without Serial works, Roverbot follow the black line.
void loop() {
start_line_follow();
}
void start_line_follow() {
if (digitalRead(10) == HIGH) {
Motor1(255, false);
Motor2(255, true);
}
else
{
Motor1(255, true);
Motor2(255, true);
return;
}
}
}
This Code with Serial doesn't works, Roverbot turn left around, but not follow the line.
0 = Stop Line Follow
1 = Start Line Follow
void loop() {
if (Serial.available() > 0) {
incomingData = Serial.read();
if (incomingData == 49) { // ASCII Code for 1
start_line_follow();
}
if (incomingData == 48) { // ASCI Code for 0
stop_line_follow();
}
}
}
void start_line_follow() {
if (digitalRead(10) == HIGH) {
Motor1(255, false);
Motor2(255, true);
}
else
{
Motor1(255, true);
Motor2(255, true);
return;
}
}
void stop_line_follow() {
Motor1(0, false);
Motor2(0, false);
return;
}
This code, I was able to start follow line, but do not stop.
void start_line_follow() {
while(1){
if (digitalRead(10) == HIGH) {
Motor1(255, false);
Motor2(255, true);
}
else
{
Motor1(255, true);
Motor2(255, true);
}
}
}
void stop_line_follow() {
Motor1(0, false);
Motor2(0, false);
}
system
April 25, 2013, 7:00pm
3
Not your problem, but if (incomingData == 49) { // ASCII Code for 1
is simpler if you write
if (incomingData == '1') {
This Code with Serial doesn't works, Roverbot turn left around, but not follow the line.
Because you call "start_line_follower", exit it, then don't call it again until you get another '1'
I was able to start follow line, but do not stop.
Code:
void start_line_follow() {
while(1){
Not surprising it didn't stop.
OMG! This sucks!
http://www.dfrobot.com/wiki/index.php/Line_Tracking_Sensor_for_Arduino_(SKU:SEN0017)
int IN2 = 4;
int EN2 = 5;
int EN1 = 6;
int IN1 = 7;
int VCC = 8;
int GND = 9;
void Motor1(int pwm, boolean reverse) {
analogWrite(EN1,pwm);
if(reverse) {
digitalWrite(IN1,HIGH);
}
else {
digitalWrite(IN1,LOW);
}
}
void Motor2(int pwm, boolean reverse) {
analogWrite(EN2,pwm);
if(reverse) {
digitalWrite(IN2,HIGH);
}
else {
digitalWrite(IN2,LOW);
}
}
void setup() {
int i;
for(i=4;i<=7;i++)
pinMode(i, OUTPUT);
pinMode(VCC, OUTPUT);
pinMode(GND, OUTPUT);
digitalWrite(VCC, HIGH); //Line Tracking Sensor for Arduino
digitalWrite(GND, LOW); //Line Tracking Sensor for Arduino
Serial.begin(9600);
delay(2000);
}
void loop() {
char val;
while(1)
{
val=Serial.read();
if(val!=-1) {
switch(val) {
case 'g':
start_line_follow();
break;
case 'h':
stop_line_follow();
break;
}
}
}
}
void start_line_follow() {
if (digitalRead(10) == HIGH) {
Motor1(255, false);
Motor2(255, true);
}
else
{
Motor1(255, true);
Motor2(255, true);
}
}
void stop_line_follow() {
Motor1(0, false);
Motor2(0, false);
}
If i remove while(1), Roverbot turn left around and can stopped it, but not follow the line.
If i set while(1), Roverbot follow the line and can't stopped it.
What the heck????
This sucks!
system
April 25, 2013, 8:52pm
7
Look at your code, get a pencil and paper, and work through what happens.
Remember, "loop()" loops, but a function may not get called again if the condition that caused it to be called no longer persists.
system
April 25, 2013, 9:17pm
9
It may surprise you, but it is virtually impossible to see what key it is you're hammering in that video.
Describe what happens, what you expect to happen and how they differ.
Or, post code.
Get rid of the while(1). Loop is called repeatedly anyway and your way is preventing serialEvent being called behind the scenes.
Take out while(1) and if(val!=-1) then fix your brackets. Also in both of your functions, right before the closing brackets, add return; See if that helps.
You should also have if(Serial.available() > 0) { /* your code here */}
Nothing works. I give up!
system
April 26, 2013, 10:12am
13
OK, thanks for letting us know.
Or, you could post your code and your observations, and we could help you.
Your call.