I have a bot set up to listen for an 'm' or an 'a' and enter a specific mode depending on if 'a' or 'm' is received. I have gotten it to work with motion control('m') and then switching to autonomous('a') but I can't get it to do the reverse. It appears that Serial.read will not pick up the characters when in autonomous mode. When I press 'm' or 'a' it doesn't even reflect in the Serial.println, though it prints the accelerometer data just as it does when the program first starts. I have run out of ideas. any suggestions would be a great help. Code attached.
(Note that's not the full code, I left out most of the variable declarations and such, as its pretty long and I wanted it to be easier to read)
AgentNoise:
(Note that's not the full code, I left out most of the variable declarations and such, as its pretty long and I wanted it to be easier to read)
Code snippets rarely result in answers.
For example, your code that checks to see if a serial character is available before calling Serial.read() is missing.
I'm not sure the Serial.available>0 will help since it receives a constant stream of data from my accelerometer so its always greater than 0, though I'm clearly wrong about something. I went ahead and loaded the whole code. Please see the attached, thank you.
float data[3] = {
3}; do you care what data[0], data[1], and data[2] are?
while(choice = 'a') should be ==
if (data[0]<9 && data[2]<-3) can floats be negative? I don't know
parseData=true;
if (parseData==true) won't this always be true, as you just set it to be?
{
I do care what data[0] data [1] data[2] are. They are the float values that come from my phones accelerometer and need to be assigned very specifically to the x,y,z values or motion control does not work at all. That's what the FloatValues() function is for, to ensure that the float values go to the right variable. Float values can be negative, I assume, since the motion control has no problems with that statement. All motion control stuff works fine.
Parse data will be true anytime the serial.available is greater than 0 but I want it to stop when it stops getting data(ie my phone goes to sleep)
My problem is currently only in the autolistener function. It for some reason will not pick up Serial.read() that isn't the accelerometer data. When I connect my phone it gets the stream of data but when I send 'a' or 'm' it doesn't seem to register, nothing gets printed to the monitor. Though if I disconnect my phone it works just fine, it prints to the monitor what I sent ('a' or 'm') and switches states.
Ok, I have fixed that, small slip up on my part. Still having the same issues, Serial.read does not seem to be picking up anything I send other than the accelerometer data when it is calling the autolistener function
AgentNoise:
I'm not sure the Serial.available>0 will help since it receives a constant stream of data from my accelerometer so its always greater than 0, though I'm clearly wrong about something. I went ahead and loaded the whole code. Please see the attached, thank you.
Because Arduino can be reading 100+ times for every character actually sent, even at 115,200 baud?
At least, if your code is tight it can.
AgentNoise:
I'm not sure the Serial.available>0 will help since it receives a constant stream of data from my accelerometer so its always greater than 0, though I'm clearly wrong about something. I went ahead and loaded the whole code. Please see the attached, thank you.
Because Arduino can be reading 100+ times for every character actually sent, even at 115,200 baud?
At least, if your code is tight it can.
Ok, I must be missing something here. If have it read:
if (Serial.available > 0)
{
choice = Serial.read();
}
it will always go straight to that because Serial.available will always be greater than 0 because it receives a constant stream of data from my phone. As far as I can tell that would just make it not work when my phone went to sleep. I am assuming I'm missing something. Mind elaborating?
If you really have got a continuous stream of data, then all those delays are going to have to go.
Even a 250 millisecond is enough time for the input buffer to overflow nearly four times over.
Great! That's good to know. I was beginning to think it was something of that nature. Having actual code running was the only difference between choose(); function and the autolistener(); I'll see if I can figure how to make it function without the delays.
I didn't go through your code. Just don't take it as a given that there will always be serial available because when you do get rid of the delays that may not be true far more often than it is.
As long as you check available() first your read() for up to available chars will be good.
However I would put the char test for 'a' or 'm' inside the if available { } along with the read so it only changes mode when a valid change is read.
At 115200 baud you have at best 11520 chars/second (serial data includes start & stop bits), just under 87 microseconds each. Okay, you can't read 100x for every one but you can read more than 20x as fast as the serial characters arrive with tight code. My guess is that your phone connects at more like 38400 baud.
I didn't go through your code. Just don't take it as a given that there will always be serial available because when you do get rid of the delays that may not be true far more often than it is.
There will always be data in this instance. I understand on other project it may not but on this particular one there will always be data and when there isn't, I don't want it to affect the autonomous movement. I do use the Serial.available>0 in motion control because I want it to stop when there is not serial data and to only run the FloatValues() function when it starts to receive data.
As long as you check available() first your read() for up to available chars will be good.
However I would put the char test for 'a' or 'm' inside the if available { } along with the read so it only changes mode when a valid change is read
I am unsure what you mean by this. Care to give an example?
void loop()
{
static byte opMode = 0; // 0 is manual, 1 is automatic, room for more
if (Serial.available > 0)
{
choice = Serial.read();
switch ( choice ) // add all the cases you want later
{
case 'a' :
opMode = 1;
// other changes that only happen when the mode is changed to automatic go here
break;
case 'm' :
opMode = 0;
// other changes that only happen when the mode is changed to manual go here
break;
}
} // end of if serial available
// the rest of your code that always runs, without any delays, uses opMode to know what it is doing
}