Hey guys, looking for some help trying to figure out what I am doing wrong. Basically, I am using an arduino board with a zigbee and using just Serial Communication to talk between the boards. I am running into a major problem with storing integers and then trying to manipulate them on the receiver end.
Here is the part of my receiver code that interprets a digit and stores it in a string:
Basically what happens is when I get to the math part of the loop, the math does not come out right in any way, shape, or form. I printed out the character string and it was giving the right values, but when I printed out the mathVar1 variable I got values that were veeeery wrong. Anyways, if anyone can help me out, I'd appreciate it very much. I tried multiple ways (such as using the parseInt command and trying to create a function that would take any values 0-9 and do math), but for some reason issues always come up. I would really appreciate the help, thank you.
From its name, the variable "integerByte" sounds like it is of type byte (value from 0 to 255) but you are assigning an integer value to it. Maybe you are declaring it as integer and it is just the name that is confusing me?
In the last line, you take a value greater than 1543 and divide it by 2023. I think that, if integerByte is an integer, then this calculation will be done as integer, not floating point. So say integerByte is 2000. 2000/2023 = 0, I think, since it will be done in integer maths and rounded down / truncated.
Can you say a bit more about how you would like mathVar1 to be calculated?
From its name, the variable "integerByte" sounds like it is of type byte (value from 0 to 255) but you are assigning an integer value to it. Maybe you are declaring it as integer and it is just the name that is confusing me?
In the last line, you take a value greater than 1543 and divide it by 2023. I think that, if integerByte is an integer, then this calculation will be done as integer, not floating point. So say integerByte is 2000. 2000/2023 = 0, I think, since it will be done in integer maths and rounded down / truncated.
Can you say a bit more about how you would like mathVar1 to be calculated?
All the best
Ray
Wooooops, I really do apologize for the confusion there. But yeah, it is not meant as a value 0 - 255, sorry about that. Here is the entirety of the code:
const int rightMotor = 8;
const int leftMotor = 9;
const int leftForward = 10;
const int leftReverse = 11;
const int rightForward = 12;
const int rightReverse = 13;
int baseSpeed;
int mathVar1;
int integerByte;
int incomingByte;
char strValue[200];
int index = 0;
boolean stringComplete = false;
void setup(){
Serial.begin(9600);
pinMode(leftForward, OUTPUT);
pinMode(leftReverse, OUTPUT);
pinMode(rightForward, OUTPUT);
pinMode(rightReverse, OUTPUT);
}
void loop(){
if(stringComplete){
if (incomingByte == 'F'){
digitalWrite(leftReverse, LOW);
digitalWrite(rightReverse, LOW);
digitalWrite(leftForward, HIGH);
digitalWrite(rightForward, HIGH);
}
else if (incomingByte == 'R'){
digitalWrite(leftForward, LOW);
digitalWrite(rightForward, LOW);
digitalWrite(leftReverse, HIGH);
digitalWrite(rightReverse, HIGH);
}
else if (incomingByte == 'A'){
baseSpeed = 0;
}
else if (incomingByte == 'B'){
baseSpeed = 150;
}
else if (incomingByte == 'C'){
baseSpeed = 255;
}
else{
integerByte = atoi(strValue);
Serial.println(integerByte);
if (integerByte >= 1543){
mathVar1 = baseSpeed - ((integerByte/2023) * baseSpeed);
analogWrite(leftMotor, mathVar1);
analogWrite(rightMotor, baseSpeed);
}
else if (integerByte <= 1480){
mathVar1 = baseSpeed - ((1 - (integerByte/1480)) * baseSpeed);
analogWrite(rightMotor, mathVar1);
analogWrite(leftMotor, baseSpeed);
}
else{
analogWrite(leftMotor, baseSpeed);
analogWrite(rightMotor, baseSpeed);
}
Serial.println(mathVar1);
}
index = 0;
stringComplete = false;
incomingByte = 0;
}
}
void serialEvent() {
while (Serial.available()) {
char inChar = Serial.read();
if (isDigit(inChar)){
if (inChar == '\n') {
strValue[index] = 0;
stringComplete = true;
}
else
{
strValue[index++] = inChar;
}
}
else{
incomingByte = inChar;
stringComplete = true;
}
}
}
holmes4:
What do the print show.
Mark
I receive the appropriate range of values for the integerByte, but the math values always indicate either 0 or 150.
I changed the values as you suggested and it definitely did pass the variables through, however, it still stays in the range of just around 150 and 0. Also, both motors seem to be affected by the change as well. Once again, I appreciate the help, Mark, it means a lot.
PaulS:
int integerByte;
int incomingByte;
When you rename these to something that makes sense, or change the type to match the name, feel free to come on back.
Hey bud, I would appreciate it if you kept it professional and would only post constructive criticism. Being disrespectful to someone for no apparent reason is unwarranted, especially if they have shown no hostility towards you.
It would have been a lot more respectful if you changed it up to "Hey, just as a heads up for when you post code, be careful with naming your variables. Some people may get confused with the names you assign certain things." Of course you can choose to take it or leave it, I would just appreciate it if you did.
I changed out the regular math to using the map() function and I am getting the correct ranged values. However, my motors still run at the exact same speed though. O.o
I changed out the regular math to using the map() function and I am getting the correct ranged values. However, my motors still run at the exact same speed though. O.o
You still have the dumb names, and you didn't post the modified code.
What is sending data to the Arduino? Can you use that to see debug output? If so, why don't you have any?
I changed out the regular math to using the map() function and I am getting the correct ranged values. However, my motors still run at the exact same speed though. O.o
You still have the dumb names, and you didn't post the modified code.
What is sending data to the Arduino? Can you use that to see debug output? If so, why don't you have any?
Basically I made a controller with a zigbee and a potentiometer tied to A0. I then Serial.print() the values and it is picked up by a remote control car that has its own zigbee and arduino. The controller coding is working perfectly, the issues I have been having is on the receiving end. It does pick up the values being put out, just for some reason when I have been trying to manipulate any of those vars an issue occurs. And right now, my final issue is figuring out why my two motors are operating at the same speeds regardless of what the coding is telling them to do. I will take a continuity tester to it to see if my soldering somehow, least likely, made a small connection across pins. Otherwise, I have no idea and it has been pissing me off. I typically wouldn't ask for help, but seeing as it has been almost a month working on this project and I only have the controller end working perfectly, I decided to try and get some.
Hackscribble:
Hi Bill
So you're saying that now, when you get to the following code, you have the correct values (in range 0 to 255) for mathVar1 and baseSpeed?
analogWrite(rightMotor, mathVar1);
analogWrite(leftMotor, baseSpeed);
What type of Arduino are you using and how is it connected to the motors? Please confirm that you are using pins 8 and 9 for rightMotor and leftMotor.
Ray
I am using a basic Sparkfun Redboard kit. It is printing out the right values and it is hooked up to the right pins, but I will take a continuity tester to my pins to see if it was a soldering problem... other then that, I have no idea. I appreciate the help though.
I have not used the Redboard. Is it the same as an Arduino Uno?
If so, I found the following in the reference on this site about analogWrite. "On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11."
The last code you posted had:
const int rightMotor = 8;
const int leftMotor = 9;
...
analogWrite(leftMotor, mathVar1);
analogWrite(rightMotor, baseSpeed);
So one thing to look at might be that use of pin 8.
Also, what type of motors are they? What connections do they have and how are they wired to pins 8 and 9 of the Redboard?
Basically I made a controller with a zigbee and a potentiometer tied to A0. I then Serial.print() the values and it is picked up by a remote control car that has its own zigbee and arduino. The controller coding is working perfectly, the issues I have been having is on the receiving end. It does pick up the values being put out, just for some reason when I have been trying to manipulate any of those vars an issue occurs. And right now, my final issue is figuring out why my two motors are operating at the same speeds regardless of what the coding is telling them to do.
I'd work on the receiving end just using the serial monitor to send typical commands to the receiving arduino for motor control troubleshooting. You may need to have some data flow control on the sending end to possibly prevent having a data flood on the receiving end.