Pages: [1]   Go Down
Author Topic: read analog sensor with arduino uno and programming C  (Read 1009 times)
0 Members and 1 Guest are viewing this topic.
Indonesia
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dear All,

I want to read data from analog sensor with arduino uno and read those data in my PC using C by serial port
I will make data are look like this one [header A] [data sensor0] .. [data sensor5] [footer] (8 data)

But the data do not stable when I read in my PC. some times the length of the data is 8 data, but sometimes less, some times more.


Any suggestion?

My arduino program:

Code:
void setup()  {
  Serial.begin(9600);
}

void loop()  {
  
  Serial.print("A ");
  delay(10);
  Serial.print(analogRead(0));
  delay(10);
  Serial.print(" ");
  delay(10);
  Serial.print(analogRead(1));  
  delay(10);
  Serial.print(" ");
  delay(10);
  Serial.print(analogRead(2));
  delay(10);
  Serial.print(" ");
  delay(10);
  Serial.print(analogRead(3));
  delay(10);
  Serial.print(" ");
  delay(10);  
  Serial.print(analogRead(4));
  delay(10);
  Serial.print(" ");
  delay(10);  
  Serial.print(analogRead(5));
  delay(10);
  Serial.print(" ");
  delay(10);  
  Serial.print(" B\n");
  delay(10);
}

and my C program:

Code:
int main(int argc, char**argv) {
    buf=malloc(4095);
    open_port();
    free(buf);
    while(1){
        read(fd,buf,60);
        printf("%s\n",buf);  }
    close(fd);
    free(buf);
   }


regard Daniel


Moderator edit: [code] [/code] tags added.
« Last Edit: November 25, 2012, 04:07:53 am by limavolt » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 640
Posts: 50335
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

More questions that answers, this time.

On the Arduino side:

Why are you delay()ing between sending data?
Do you really have something connected to all 6 analog pins?
Why are you sending 2 spaces at the end?

On the PC side:

Why do you assume that all the data has arrived at once, especially as you are taking forever and a day sending it?

You should be reading and printing one character at a time from the serial port, in a loop. When the character is a B, also print a carriage return and start the loop over again.
Logged

Indonesia
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank for the advise Pauls,

I put delay betwen sending data because I will have sensors on the pins analog in, I assume that the adc need time to read the data.

and in the end I put the "  " (spaces) in the end of each data because I need to identify data. So every data has space between others.

Do you have some idea for the aduino side?

For in the PC side:
I assume that PC will get all the data at once, but If I see the result may be my assume is not right.

I will try to read every line and return to the loop.

regards

daniel
Logged

Indonesia
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

if I change with this one

on arduino:
Code:
int inbyte = 0;
void setup()  {
  Serial.begin(9600);
}

void loop()  {
  inbyte = Serial.read();
  
  if(inbyte=='0'){
    Serial.print(analogRead(0));
    delay(10);
    Serial.print(" ");
    delay(10); }
  
  if(inbyte=='1'){
    Serial.print(analogRead(1));  
    delay(10);
    Serial.print(" ");
    delay(10);}
}

on C:

Code:
while(1){
        open_port();
        
        write(fd,"0",1);
        read(fd,buf0,10);
        printf(" %s \n",buf0);
        usleep(100);
        
        write(fd,"1",1);
        read(fd,buf1,10);
        printf(" %s \n",buf1);
        usleep(100);
}

There are no result on C, but when I open the serial port monitor on arduino. The results appear on the C program.

but the result is weird.

I Put 0 in input 0 and put 5 v (1023) in input 1.
in serial monitor appear 102 0
in c appears
3
(empty)

sometimes
in serial monitor 10 0
in c appears
23
(emty)



« Last Edit: November 26, 2012, 01:10:25 am by limavolt » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 640
Posts: 50335
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
There are no result on C, but when I open the serial port monitor on arduino. The results appear on the C program.
Connecting two applications to the same serial port is a recipe for disaster. You didn't say that that was your goal.

The fact that opening the Serial Monitor application starts the C application receiving data means that the C application is not opening the serial port correctly.

I'll only say this once more. GET RID OF THE DELAY()S.

I'll only say this once more. Serial data transmission is ssslllooowww. You must read and store every character until the end of packet marker arrives. THEN, you can print the stored data. NOT before then, no matter how long you have waited.

Logged

Pages: [1]   Go Up
Jump to: