Serial Data with ATmega168

Hi everybody, i started to program with arduino atmega 168. I just want to use a data in the serial port :

If i received "Data1 : 35.2" i want to store 35.2 in a variable. If anybody help me, i don't understand how the serial libary works

Thanks $ Mont

Hi Mont, and welcome

I think the best way to understand what's going on with Serial is to run through some of the available tutorials. This one (http://www.jeremyblum.com/2011/02/07/arduino-tutorial-6-serial-communication-and-processing/) talks about serial communications with a PC, though it would be best to start at the start, see the previous 5 so you know what the basics are about first.

The Serial library is covered in these reference pages http://arduino.cc/en/Reference/Serial also.

For the example you request, you'll simply be doing a Serial.read() and storing the value in a variable. How you'll do this is largely a function of what is sending the data to you. If you're just typing into the serial monitor to get this information that will be only slightly different to if you're receiving the data from a serially connected wireless module, so the good news is once you've learned about Serial, it's going to be very useful to you overall.

Cheers ! Geoff

i have to read two data : Data1,Data2. Data1 go on a LCD and the other power on leds

I receive this : A35.2,E165.4 And i want to store 35.2 in a variable and 165.4 go on a 7Segment

Hi,

How are you receiving it if you've not already stored it in a variable? Please post your sketch here so we can see how far you've got so far. (use the # button and paste it in between the tags).

Cheers ! Geoff

char buff[11]="";
char bytein;
int i;
int j;
int compt; 
int comptb;

void setup(){
  Serial.begin(9600);
}
void loop()
{  
  
  if (Serial.available() > 0) 
  {  
     bytein = Serial.read();  
   else {
      buff[compt]=bytein;
      compt++;
        if (compt==11){
        compt=0;
        for(i=0,i<16,i++){
          
        if (buff[i]==",")
        {
        serial[8] = i;
        comptb++;
        }
      
      for (i=0;i<8,8++)
      {
        switch(i){
          case 0 : data1;break;
          case 1 : data2;break;
        }
  
      }  
      }
      }
  }
  
  delay(100);
  }
}

Hi

I found that doesn’t compile. You’ve made a bit of a mess with your { and } not matching up. I think this helps illustrate what you’re trying to do though.

This line includes a variable that’s not defined anywhere, the array serial,serial[8] = i;And I gather the final loop your intent is to go through your buffer and extract the values whereas what I see there is pseudocode only. My thoughts there are you don’t need a buffer since you already have the Serial buffer so you can work directly from there if you know what shape your data is going to arrive in.

But that’s the crucial part…you’re writing this as if your data is arriving as a comma delimited string. Before proceeding are you sure this is the case? What is it being generated by?

Will the data be a continuous stream of A,E,A,E,… or will it come through in pairs with a newline in between? Will the values always be prefixed by the A & E alphas or was that just for your example (since your code doesn’t look for the alpha)?

Cheers ! Geoff

Data from a microcontroller that sends me the information, however it is possible to change the transmission format. I chose to separate my data by ",". I'll post the schematics tonight. I based on a program to read a GPS Data.

I found this on the arduino’s forum :

void readserial()
{
 while (Serial.available()) {
    delay(10); 

    char c = Serial.read(); 
    if (c == ',') {
      break;
    }  
    readString += c; 
  }   

  if (readString.length() >0) {
    Serial.println(readString); 
    readString=""; 
  }
}

ok this code is working but i can’t to translate the reading ascii value to an integer :

void readata1()
{
 
  while(Serial.peek()== -1);
  {
  byteSer=Serial.read();         // Read a byte of the serial port
   if (byteSer == -1) {           // See if the port is empty yet
     delay(100); 
   } else {
     Serial.print(byteSer);
     buffer[compteur]=byteSer;        // If there is serial port data, it is put in the buffer
     compteur++;
     if (byteSer == ','){
     for(int i=0;i <5;i++)
     {
      data1=buffer[i];
      
           
     }
     }
    }                 
  }
}

Hi,

Here’s a snippet that does what I think you want. After uploading, launch the serial monitor and key in the data like it’s coming from serial port stream. The values are output (as floats) back to you on the serial monitor.

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

void loop() {
  float returnValue;

  while(!Serial.available()) delay(100);            // do nothing until we have something to process

  char c = Serial.read();                           // take in the next character from the buffer
  
  // switch/case is overkill for two options...but easy to extend if you add more record types
  switch(c) {
  case 'A':                                        // the A prefix
    returnValue = readData();                      // populate returnValue with the float following the prefix
    // do stuff with returnValue based on the A prefix
    Serial.print("A: ");
    Serial.println(returnValue);
    break;

  case 'E':                                        // the E prefix
    returnValue = readData();                      // populate returnValue with the float following the prefix
    // display on 7seg as you have an E prefix
    Serial.print("E: ");
    Serial.println(returnValue);
    break;

  case ',':                                       // delimiter. Ignore.
  // Nothing to see here (but can add debugging code if you like)
  // this should only be triggered if there's a delimiter at the start of the Serial buffer
  // or if there's 2 in a row, as readData() disposes of each subsequent delimiter.
  // You could add more cases to deal with other special characters though...
    break;

  default:                                        // something else 
    // print out warning
    Serial.print("Warning: unexpected character [");
    Serial.print(c);
    Serial.println("] ignored.");                                
    break;
  }
}  // rinse, repeat

// read from serial buffer until a delimiting comma is found, or the timeout occurs
float readData() {
  char workBuffer[20];                              // workign string buffer
  char thisChar;
  int idx = 0;                                      // index to workBuffer[]
  unsigned long startTime = millis();               // the time, now
  unsigned long timeOut = 5000;                     // number of milisseconds to wait after last character before timing out

  while(startTime - millis() < timeOut && idx < 20) {
    if(Serial.available()) {
      thisChar = Serial.read();
      if(thisChar == ',') startTime += timeOut;     // end of value, force end of reading loop
      else workBuffer[idx++] = thisChar;            // else store the character in the buffer
    }
  }
  workBuffer[idx] = '\0';                           // null terminate the string buffer
  return atof(workBuffer);                          // convert to float.
}

Clearly, you’ll want to remove anything that says Serial.print() or Serial.println() from yours.

Hopefully this gets you on track,
Cheers ! Geoff

It works perfectly thank you so much !!!
I hope i can help you next time.

See you soon