As you may know i have built a maze solving robot that does left hand on the wall currently. I have been trying to add the refining part of the code where it figures out the best path to the end without going down the dead ends (i hope you understand what i mean). Well to do that i need a string of chars and a pointer to tell where to store each char which is a turn like 'L' , 'R', 'B' or 'S'. Ok so every time i turn i need to add 1 to the pointer which i call "path_length" meaning the length of the path ie the number of turns the robot has taken. So we need to add 1 where the turning function is. Here is a example.
void turnl(){ // this is the function to turn left
if(t==0){ //my attempt to regulate how many times we add 1 to the path length
path[path_length]='L'; // this stores 'L' in the path and the pointer place called "path_length"
path_length+=1; // this adds 1 to the pointer so we can store the next turn in the next spot
t+=1; // now t = 1 and we wont add 1 again until t =0
}
digitalWrite(out1, LOW);
digitalWrite(out2, HIGH);
digitalWrite(out4, HIGH);
digitalWrite(out3, LOW);
x2 =digitalRead(cen);
if(x2==0){ // it continues to turn until the middle sensor is back on the line meaning we are done turning
turnl();
}}
since i use the sensor to decide when we are done turning, like you should, and not a delay, we will go through this loop many times until we are done turning. This is why i needed to regulate it to add 1 only once. But some how i needed to reset t to = 0 again else we would never be able to add 1 again and there for it would only say we did 1 turn. I tried to reset t in the drive straight function but it turns out while turning it seems to go through this function as well and resets it. So i put a loop counter so it would only reset it after its been through the loop a lot ie actually driving straight and done turning like this.
void drive(){
master+=1;
if(master>750){
t=0;
}
digitalWrite(out1, LOW);
digitalWrite(out2, HIGH);
digitalWrite(out3, HIGH);
digitalWrite(out4, LOW);
}
it didnt really work. Any ideas or suggestions on a better way to get it to work or a way to fix what i have tried to do?
I’ll admit that I haven’t
I’ll admit that I haven’t been writing any code for Arduino or PICaxe yet so I may be wrong here, but I would expect the program to have to exit out of every single call it has made to turnl(). Therefore I think it would work if you set t = 0 after the last if statement, like this.
void turnl(){ // this is the function to turn left
if(t==0){ //my attempt to regulate how many times we add 1 to the path length
path[path_length]=‘L’; // this stores ‘L’ in the path and the pointer place called "path_length"
path_length+=1; // this adds 1 to the pointer so we can store the next turn in the next spot
t+=1; // now t = 1 and we wont add 1 again until t =0
}
digitalWrite(out1, LOW);
digitalWrite(out2, HIGH);
digitalWrite(out4, HIGH);
digitalWrite(out3, LOW);
x2 =digitalRead(cen);
if(x2==0){ // it continues to turn until the middle sensor is back on the line meaning we are done turning
turnl();
}
t = 0;
}
didnt think of that…will
didnt think of that…will try it
Here is one problem I see
Here is one problem I see with the code. I’m unsure of where t is as far as the scope of the function. If ‘T’ is only going to be used in this functions scope you should initialize it here like so. ie
int t =0;
if its a global var then just t = 0;
You also have a few things going on in the function that might cause issues. The first if statement is run once, then the digital write code is run, then you check the sensor and do a recursive call (turnL()). That recursion might be causing an issue especially since you didn’t initialaize the value of T to 0 at the start of the function(but again, I don’t know since this is a small block of a much larger program).
Based on my understanding of the arduino syntax, all of the variables that you are using(out1-4,cen, x2) are global, but since I dont’ know what they were initialized to, it’s hard to figure out whats going on.This is relevant to figuring out coding issues.
After looking at your function I would suggest something like this which may help streamline it:
void turnl(){
path_length = size_of(path) +1;
path[path_length]=‘L’; // this stores ‘L’ in the path and the pointer place called “path_length”…which doesn’t really need to be a pointer.
//since we keep turning, we don’t need to worry about adding a 1 to t.
while x2 > 0 { //keep turning until we hit 0
digitalWrite(out1, LOW);
digitalWrite(out2, HIGH);
digitalWrite(out4, HIGH);
digitalWrite(out3, LOW);
x2 = digitalRead(cen); //read until x2(global var??) is 0
}
//do whatever else we need to before exiting the funciton like reset your out1-4 pins here (if you need to).
//no recursion since we don’t call turnl() again
}
Let me know if this doesn’t make sense to you.
Looks good voodoo.I suspect
Looks good voodoo.
I suspect that path_length is not a pointer at all, although I can’t be sure without seeing the initialisation. Arrays expect an integer as their index, not a pointer, and the dereference operator hasn’t been used. If path_length is indeed a pointer, it’s likely that path[path_length] would generate a type mismatch or out of range error.
Most likely this is just the result of some confusion between the terms ‘pointer’ and ‘index’.
Thanks TF, I appreciate the
Thanks TF, I appreciate the second set of eyes! Yeah, I wasn’t certain as to the use of the pointer in this case as an index as it didn’t make sense to me. I’m also not super familiar with using pointers, but from I do understand of them, again, it didn’t seem to fit what could best be used for what was going on in the code.
Btw, would it make sense to make path_length a local int var? That would seem to work imo.
i will try to try this today
i will try to try this today if i can. Let me explain a bit more to help. Well not too sure what to say but the path_length tells where to store the turn, ie "L", in the array (whatever name you want to call it, thats what its used for in my case). X2 is the middle line sensor so when it = 0 we know we are done turning. Thanks for the code, it looks good and i do not know why i did not think of this. Will tell you how it goes and if i need to later, i can post more code.
Sounds cool. I haven’t tried
Sounds cool. I haven’t tried the code myself, and I may have misunderstood something actually. the sizeof function would return the number of bytes, which might not be what you want. I was thinking that it would return the count of the array. After looking up more info on the funciton, I found an interesting posting that mentioned it.
for some reason I can’t link the url so here it is. http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1260706526/3
as well as this one that mentions strlen related to an issue
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1256149583
just google arduino and strlen to find more info.
so strlen()might work better, but as you know, testing this is the key.
EDIT:
you could possibly write a small c program that could run through testing the strlen function to see if it works for what you need.
You’ll also need to include #include <string.h>
http://www.nongnu.org/avr-libc/user-manual/group__avr__string.html#g7fd4936b86eb6b87e98587044c562715
Been a while since I did any
Been a while since I did any proper C/C++ coding, I always forget that a string is initialised as a pointer to the array by default. strlen() and the other true string functions are definitely the way to go.
From your description PM, path_length is definitely a regular index integer, and not a pointer. To summarise the different: an index tells you where the data is in an array, while a pointer tells you where the data is in the memory. An array is something that exists only conceptually in the code, while the memory location is a location in the physical memory hardware.