When I use serial monitor to check data does it show the negative sign in a number?
In monitor the value shows up as 127 instead -127
tempFB = map(FB, 0, 1024, -127, 127);
tempLR = map(LR, 0, 1024, -127, 127);
When I use serial monitor to check data does it show the negative sign in a number?
In monitor the value shows up as 127 instead -127
tempFB = map(FB, 0, 1024, -127, 127);
tempLR = map(LR, 0, 1024, -127, 127);
Your code is incomplete.
Sorry thought that was enough info.
here ya go
// Analog Input Variables
int FB; // Forward and Back
int LR; // Left and Right
//int Spin; // Rotation (used on vectoring robots)
// Temporary variables used in calculations
double tempFB, tempLR, tempSpin;
// Transmitted packet
byte byteFB;
byte byteLR;
byte Digital1;
byte checksum;
// Reverse bits
bool revA1 = 1; // These are used to easily flip
bool revA2 = 0; // the direction of the joystick
// Misc variables
int deadband = 15
;
void setup() {
Serial.begin(9600); // COM to xBee
}
void loop() {
reinitVars(); // nulls out temporary variables
captureData(); // retrieves new data
packetizeData(); // assembles data to be sent
//transmitData(); // send data off to xBee
debugData(); // print out formatted debug data
delay(20); // Delay needed for consistant communication
}
// Reinitialize temporary variables
void reinitVars() {
Digital1 = 0;
tempFB = 0;
tempLR = 0;
}
// Retrieve analog input data and conform to specs
void captureData() {
FB = analogRead(A1);
LR = analogRead(A2);
// Reverse input analogs if flagged
if (revA1) {
FB = 1024 - FB;
}
if (revA2) {
LR = 1024 - LR;
}
// The switch is towards the joystick (full speed)
tempFB = map(FB, 0, 1024, -127, 127);
tempLR = map(LR, 0, 1024, -127, 127);
if ((tempFB > (0 - deadband)) && (tempFB < deadband) )
{
tempFB = 0;
}
if ((tempLR > (0 - deadband)) && (tempLR < deadband) )
{
tempLR = 0;
}
}
// Assembles data to be sent
void packetizeData() {
// Shift analog data up so we have a range of
// 0-255
//tempFB = tempFB + 127;
//tempLR = tempLR + 127;
// Assemble the digital packet
// Add status of switches to digital1 here
// convert transmitted data to bytes
byteFB = byte(tempFB);
byteLR = byte(tempLR);
// calculate checksum
checksum = byteFB + byteLR + byte(Digital1);
}
// xBee Communication
void transmitData() {
Serial.write("S");
Serial.write("D");
Serial.write("R");
Serial.write(byteFB);
Serial.write(byteLR);
Serial.write(byte(Digital1));
Serial.write(checksum);
}
// Print out values for easy debugging
void debugData() {
Serial.print("[FB: "); Serial.print(byteFB); Serial.print("]");
Serial.print("[FBraw: "); Serial.print(FB); Serial.print("]");
Serial.print("[LR: "); Serial.print(byteLR); Serial.print("]");
Serial.print("[LRraw: "); Serial.print(LR); Serial.print("]");
//Serial.print("[D1: "); Serial.print(byte(Digital1), BIN); Serial.print("]");
Serial.println("");
}
Were you expecting to see a negative number from an unsigned datatype?
So do I need to change the two transmitted Byte to integers?
byteFB = byte(tempFB);
byteLR = byte(tempLR);
to
byteFB = int(tempFB);
byteLR = int(tempLR);
thanks again.
My saber tooth motor driver need to see -127 to +127 to drive the motors forward and backwards. 127 full speed forward, -127 full speed backwards.
In serial monitor I can see the data from the joystick move. joystick up 127 joystick down 127 how do i know that is a -127.
Will serial monitor show the negative sign?
Yes, if the variable being printed is signed.
Try this program:
void setup() {
int a = 65535;
unsigned int b=65535;
Serial.begin(9600);
Serial.print("a = ");
Serial.println(a);
Serial.print("b = ");
Serial.println(b);
}
void loop() {}
So how do i change the code to make it a unsigned integer??
I just name it unsigned Intger
byteFB = unsigned int(tempFB);
byteLR = unsigned int(tempLR);
Use your favorite search engine to look up "declare variables c/c++".
jdolecki:
So how do i change the code to make it a unsigned integer??I just name it unsigned Intger
byteFB = unsigned int(tempFB);
byteLR = unsigned int(tempLR);
You don't want unsigned variables. Those, by definition, can NOT be negative. A 'byte' is 'unsigned char' which goes from 0..255. A 'char' is signed so it goes from -128...127. An unsigned int goes from 0...65535 while an a signed int (or just int) goes from -32768...32767
blh64:
A 'char' is signed so it goes from -128...127.
There's no guarantee that char
is signed. It's implementation defined. Some Arduino targets have signed char
and some have unsigned char
.
Use signed char
(or int8_t
) if you want a signed 8-bit variable.