I have two issues for which I'm not sure the solutions. First, the following code seems to be truncating my ints so I get a max value of somewhere around 180, rather than 255. Any idea what I'm doing wrong here? Ignore the variable uip_appdata- this is part of the WiShield library (ugh), references to the rest of which have been omitted for simplicity.
int sensors[10] = {0,0,0,0,0,0,0,0,0,0};
int motors[10] = {0,0,0,0,0,0,0,0,0,0};
void setup()
{
Serial.begin(57600);
}
void loop()
{
for(int i = 0; i < 10; i++){
sensors[i] = map(analogRead(i), 0, 1023, 0, 255);
}
}
extern "C"
{
static void send_data(void)
{
memcpy(uip_appdata, &sensors, sizeof(sensors));
memcpy(&motors, uip_appdata, sizeof(sensors));
for(int i = 0; i < 10; i++){
Serial.println(motors[i]);
}
}
}
Second, trying the same thing with either chars or unsigned chars is giving me all zeros upon conversion back to ints. I think the root of both these issues is that integers take up two bytes and I'm losing the second one, but it seems I should be able to fit an integer into one unsigned char, no?
I'm also wondering if there's a more robust C function to convert chars to ints than atoi. I much prefer sprintf to itoa, as I have had problems with the latter handling larger integers (not longs, just something in the range of 1023).
unsigned char sensors[10] = {0,0,0,0,0,0,0,0,0,0};
unsigned char motorsTemp[10] = {0,0,0,0,0,0,0,0,0,0};
int motors[10] = {0,0,0,0,0,0,0,0,0,0};
void setup()
{
Serial.begin(57600);
}
void loop()
{
for(int i = 0; i < 10; i++){
sensors[i] = byte(map(analogRead(i), 0, 1023, 0, 255));
}
}
extern "C"
{
static void send_data(void)
{
memcpy(uip_appdata, sensors, sizeof(sensors));
memcpy(motorsTemp, uip_appdata, sizeof(sensors));
for(int i = 0; i < 10; i++){
motors[i] = atoi((const char*)motorsTemp[i]);
Serial.println(motors[i]);
}
}
}
OR
int sensors[10] = {0,0,0,0,0,0,0,0,0,0};
int motors[10] = {0,0,0,0,0,0,0,0,0,0};
void setup()
{
Serial.begin(57600);
}
void loop()
{
for(int i = 0; i < 10; i++){
sensors[i] = map(analogRead(i), 0, 1023, 0, 255);
}
}
extern "C"
{
static void send_data(void)
{
char str[20];
sprintf(str, "%i", sensors);
memcpy(uip_appdata, str, strlen(str));
memcpy(str, uip_appdata, strlen(str));
for(int i = 0; i < strlen(str); i++){
motors[i] = atoi((const char*)str[i]);
Serial.println(motors[i]);
}
}
}
Thanks!
-Zach