loading
 
RoboRealm+arduino Serial to integer
teamxiyo from United States  [3 posts]
10 year
Im trying to convert the COGX value into an integer to display on a 16x2 lcd this is the code i have and im not getting anything but "0" on lcd. lcd is tested and works.

#include <stdl.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int distance = 0;
int i = 0;
char incomingData[4] = {0, 0, 0, 0};


void setup()
{
lcd.begin(16, 2);
Serial.begin(9600);
}
void loop()
{
  
while (i < 4)
{
     incomingData[i] = Serial.read(); // Assign the input value to the incomingData buffer
     i++; // Increment the counter
     incomingData[i] = '\0';
     distance=atoi(incomingData);
   }
  
  lcd.print(distance);
  delay(500);
  lcd.clear();

delay(50);
}
Steven Gentner from United States  [1446 posts] 10 year
What you are using to send the information to the arduino? It seems strange that you are sending 4 byte ascii characters ... are you sure that you are instead not sending a 4 byte binary number? In that case, the atoi would not know what to do and just return 0 all the time.

Also, move the atoi outside the loop ... you only need to do it once when the data is complete ... but that will not solve your problem. Also incorrect is that the i will end at 4 ... which will exceed your buffer size (needs to be 5). So to just correct what you have would be

#include <stdl.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int distance = 0;
int i = 0;
char incomingData[5] = {0, 0, 0, 0, 0};

void setup()
{
  lcd.begin(16, 2);
  Serial.begin(9600);
}

void loop()
{
  while (i < 4)
  {
     incomingData[i] = Serial.read(); // Assign the input value to the incomingData buffer
     i++; // Increment the counter
   }
  
   incomingData[i] = '\\0';
   distance=atoi(incomingData);

   lcd.print(distance);
   lcd.home();
}

You also don't want to delay this loop since that may miss incoming characters. You just want to overwrite what is currently bring printed. Delaying for 500ms is a huge delay!

If you are sending a 4 byte number instead you would use

#include <stdl.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

long distance = 0;

void setup()
{
  lcd.begin(16, 2);
  Serial.begin(9600);
}

void loop()
{
   distance = Serial.read();
   distance |= Serial.read()<<8;
   distance |= Serial.read()<<16;
   distance |= Serial.read()<<24;
  
   lcd.print(distance);
   lcd.home();
}

or you may have to reverse (start with 24 and decrease) the << based on little or big endian.

STeven.
teamxiyo from United States  [3 posts] 10 year
Im using Serial on roborealm. In the console COG_X is working pefectly but when i send it to the arduino to display on the lcd it will not work. The first example you wrote back shows 0 at all times. The second example show -120 to about-180. I just want cogx to show on the lcd screen.

thanks alot for your help steven i appreciate it.

is this code not feasible using serial?

#include <stdl.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(6,7,5,4,3,2);

int distance = 0;
void setup()
{
lcd.begin(16, 2);
Serial.begin(9600);
}
void loop()
{
  distance = Serial.read(); // Assign the input value to the incomingData buffer
  lcd.print(distance);
  lcd.home();
}
Steven Gentner from United States  [1446 posts] 10 year
Can you post your robofile?

STeven.

This forum thread has been closed due to inactivity (more than 4 months) or number of replies (more than 50 messages). Please start a New Post and enter a new forum thread with the appropriate title.

 New Post   Forum Index