Show Posts
Pages: 1 [2] 3
16  Using Arduino / Programming Questions / Re: Convert String From Serial Data to Numerical Value on: January 16, 2013, 06:20:08 pm
I'm not understanding why I'm unable to convert the Liters/Minute to a numberical value.

Quote
7.320,4.734,284.057
4.7340 Liters/Minute
0  in HEX


Code:
   if (sensor_stringcomplete){                                                 //if a string from the Atlas Scientific product has been received in its entirety
      Serial.println(sensorstring);                                             //use the hardware serial port to send that data to the PC

     
  for (int i=6; i<11; i++)  {
    Serial.print(sensorstring[i]);     //print each string character to verify
    float flowLong = (sensorstring[i] - '0');
                }
    Serial.print(flowLong);Serial.println(" Liters/Minute");
    if (flowLong < 4.5) {
    Serial.println("FLOW WARNING!!!");
    }
       Serial.print(flowLong,HEX);Serial.println("  in HEX");
       sensorstring = "";                                                      //clear the string buffer:
       sensor_stringcomplete = false;                                          //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
       }



17  Using Arduino / Programming Questions / Re: Convert String From Serial Data to Numerical Value on: January 16, 2013, 03:09:13 pm
This will take some time to soak in.

Thanks
18  Using Arduino / Programming Questions / Re: Convert String From Serial Data to Numerical Value on: January 16, 2013, 02:51:08 pm
You need to be clearer about what you want to do. There is a huge difference between a string and a String. The string is not a class and does not have a toInt() method.

Frankly, you should not be using the String class at all. It has a major problem that is going to, sooner or later, bite you.

Now I'm really lost. My goal is to convert the second string to a value so I can write some algorithms.
0.000,0.000,0.000

This looks like it's converting each ASCII character

String : ASCII

0 : 48
.  : 46
0 : 48
0 : 48
0 : 48


Ref: http://arduino.cc/en/Tutorial/ASCIITable

I know this doesn't solve my conversion problem but I'm practicing difference string conversions.
19  Using Arduino / Programming Questions / Re: Convert String From Serial Data to Numerical Value on: January 16, 2013, 02:40:39 pm
Phew, where to begin...
...

Thanks Arrch,
I would like convert this into a floating number but I'm having a hard time finding the Reference syntax in Arduino for the following C equivalents? Would you happen to know where I can find them?

Code:
atol (Convert string to long integer) obvoiusly this is "toInt()"
atof (Convert string to double)
strtol (Convert string to long integer)
20  Using Arduino / Programming Questions / Re: Convert String From Serial Data to Numerical Value on: January 16, 2013, 01:09:48 pm
I'm not home right now, but I will give you a sample as soon as I can.

Edit: I was able to get this from a post I did a while back. Wow my iPhone likes to stretch things out. Sorry about that.


Code:
/*
control test
 */
#include<string.h>
int DRV1,DRV2,STRR,STRL;
int x = 0;
int y = 0;
int s = 0;
...




Hi HazardsMind,

I went through the sample you suggested and noted what "I think" each line does. The only one which I am figuring out are:

Quote
1. void move(int x = 0,int y = 0,int s = 0);  //Not sure why this needs to be done ???

Quote
2. move(x, y, s); // DO NOT UNDERSTAND "move()"  


Code:
//from http://arduino.cc/forum/index.php?topic=142777.0

/*
control test //name of program
 */
#include<string.h> //name of library needed
int DRV1,DRV2,STRR,STRL; //declare 4 integers
int x = 0; //intialize x
int y = 0; //initialize y
int s = 0; //initialize s
void move(int x = 0,int y = 0,int s = 0);  //Not sure why this needs to be done ???
//double z; //double the z variable (for more precision)
String val = ""; //set some buffer aside for val
String  X= ""; //set some buffer aside for X
String  Y= ""; //set some buffer aside for Y
String  state = ""; //set some buffer aside for state
//int state = 0; //initialize state to 0
int currentCommand = 0; //initialize current command to 0

int ledpin = 13; //set LED pin
byte Mopen = 4; //sets Mopen to B0100
byte Mclosed = 2; //sets Mclosed to B00010
byte M1L = 3;// PWM
byte M2L = 5;// PWM
byte M1R = 9;// PWM
byte M2R = 6;// PWM

void setup()
{
  pinMode(ledpin, OUTPUT);  // pin 13 (on-board LED) as OUTPUT
  pinMode(Mopen, OUTPUT);  // pin Mopen is set as an output                              
  pinMode(Mclosed, OUTPUT); // pin Mclosed is set as an output
  pinMode(M1L, OUTPUT);     //pin MIL is set as an output
  pinMode(M1R, OUTPUT);  // in MIR is set as an output
  pinMode(M2L, OUTPUT);  //pin M2L is set as an output
  pinMode(M2R, OUTPUT); // pin M2R is set as an output
  Serial.begin(9600);       // start serial communication at 115200bps

}

void loop() {
  if( Serial.available())       // if data is available to read, this is how to check the serial part for strings
  {
    digitalWrite(ledpin, HIGH); // if data is detected set the pint set to ledpin to HIGH (ON)
    char c= Serial.read(); // the string in serial read() is buffered in to c as characters
    if (c == ','){    //look at the incoming chars, if it is a ',' then switch the case
      currentCommand++;  //for some reason currentCommand increments????
    }
    else {   //if it is not ',' then store chars in string, keep storing the string until a , is found
      val += c;  // same as (val + c = c)val is the next string + the previous character stored in buffer
      //Serial.println(val);
      switch (currentCommand) { // for each string, currentCommand appears to increment by 1

      case 0:  //in the even there was only 1 string before the ',' X is equal to the val in buffer and val buffer is cleared ""
        X += val; // same as (X + val = val), new variable X is equal to the whole string val before the ','
        val = ""; // clear the information in the val buffer
        break; // break out of case 0
 
      case 1: //if currentCommand equals 1
        Y += val; // same as (Y + val = val), Y is now the string waiting in the val buffer
        val = ""; // clear out the val buffer
        break; //get out of case 2

      case 2:
      //Serial.println("X: "+X);
      //Serial.println("Y: "+Y);    
        state = val;   // this will only receive 1 value
        currentCommand = 0; //reset currentCommand counter to 0
        val = ""; // clear out the val buffer
        //Serial.println("state: "+state);
        //Serial.println();
        x=X.toInt();  //string to int , convert string X to integer
        y=Y.toInt();  //string to int, convert string Y to integer
        s=state.toInt();  //string to int, convert state to integer
        X=""; Y=""; state=""; //clear the X, Y and state buffer information
        move(x, y, s); // DO NOT UNDERSTAND "move()"  
        break; // get out of case 3
      }
    }
  }
}
//***ALL THIS STUFF BELOW IS FOR SERVO MOVEMENT
//***SENDS OUT VOLTAGE VALUES
  void move(int x, int y, int s)
  {  
      x=constrain(x, -90, 90);
      y=constrain(y, -90, 90);

  //Movement varibles
  
  int DRV2 = map(x, -90, 0, 255, 0);
  int DRV1 = map(x, 0, 90, 0, 255);
  int STRR = map(y, -90, 0, 255, 0);
  int STRL = map(y, 0, 90, 0, 255);  

  if(x > 0)//forwards              
  {
    //Serial.println("Driving");
    analogWrite(M1L, abs(DRV1 - STRL)); analogWrite(M1R, abs(DRV1 - STRR));  
    analogWrite(M2L, 0); analogWrite(M2R, 0);  
  }
  else if(x < 0)//backwards              
  {
    //Serial.println("Driving");
    analogWrite(M1L, 0); analogWrite(M1R, 0);  
    analogWrite(M2L, abs(DRV2 - STRR)); analogWrite(M2R, abs(DRV2 - STRL));  
  }
  else if(x == 0 && y >0)//right              
  {
    //Serial.println("Driving");
    analogWrite(M1L, STRL); analogWrite(M1R, 0);  
    analogWrite(M2L, 0); analogWrite(M2R, STRL);  
  }
  else if(x == 0 && y < 0)//left              
  {
    //Serial.println("Driving");
    analogWrite(M1L, 0); analogWrite(M1R, STRR);  
    analogWrite(M2L, STRR); analogWrite(M2R, 0);  
  }

  else //full stop
  {
    digitalWrite(M1L, LOW); digitalWrite(M1R, LOW);        
    digitalWrite(M2L, LOW); digitalWrite(M2R, LOW);    
  }

   if(s == 1)
    {
      //Serial.println("Claw Opening");
      digitalWrite(Mopen, HIGH); digitalWrite(Mclosed, LOW);
    }
    if(s == 2)
    {
      //Serial.println("Claw Closing");
      digitalWrite(Mopen, LOW); digitalWrite(Mclosed, HIGH);
    }
    if(s == 0)
    {
      digitalWrite(Mopen, LOW);  digitalWrite(Mclosed, LOW);
    }
   else{
    digitalWrite(ledpin, LOW);
    x=0; y=0; s=0;
  }
 }





21  Using Arduino / Programming Questions / Re: Convert String From Serial Data to Numerical Value on: January 14, 2013, 07:24:34 pm
Oh you want sensorstring.toInt(), not sensorstring[ i ].

Hi HazardMind,
I'm looking for some sample code to see how this is used. Is this something similar to what you suggest?

http://electronics.stackexchange.com/questions/38845/arduino-serial-read-to-int
22  Using Arduino / Programming Questions / Re: Convert String From Serial Data to Numerical Value on: January 14, 2013, 06:22:16 pm
Sorry, it's probably time to show us all your code, please.  I was guessing that sensorvalue was char[] not String.

-br


No Problem, here it is:


Code:

#include <SoftwareSerial.h>                                                    //add the soft serial libray
#define rxpin 2                                                                //set the RX pin to pin 2
#define txpin 3                                                                //set the TX pin to pin 3


SoftwareSerial myserial(rxpin, txpin);                                         //enable the soft serial port


String inputstring = "";                                                       //a string to hold incoming data from the PC
String sensorstring = "";                                                      //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false;                                          //have we received all the data from the PC
boolean sensor_stringcomplete = false;                                         //have we received all the data from the Atlas Scientific product


  void setup(){                                                                //set up the hardware
     Serial.begin(38400);                                                      //set baud rate for the hardware serial port to 38400
     myserial.begin(38400);                                                    //set baud rate for software serial port to 38400
     inputstring.reserve(5);                                                   //set aside some bytes for receiving data from the PC
     sensorstring.reserve(30);                                                 //set aside some bytes for receiving data from Atlas Scientific product
     }
 
 
 
   void serialEvent() {                                                         //if the hardware serial port receives a char
               char inchar = (char)Serial.read();                               //get the char we just received
               inputstring += inchar;                                           //add it to the inputString
               if(inchar == '\r') {input_stringcomplete = true;}                //if the incoming character is a <CR>, set the flag
              } 
 
 
 
 void loop(){                                                                   //here we go....
     
  if (input_stringcomplete){                                                   //if a string from the PC has been recived in its entierty
      myserial.print(inputstring);                                             //send that string to the Atlas Scientific product
      inputstring = "";                                                        //clear the string:
      input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
      }
 

  while (myserial.available()) {                                               //while a char is holding in the serial buffer
         char inchar = (char)myserial.read();                                  //get the new char
         sensorstring += inchar;                                               //add it to the sensorString
         if (inchar == '\r') {sensor_stringcomplete = true;}                   //if the incoming character is a <CR>, set the flag
         }
 
   for (int i=6;i<11;i=i+1) {
        Serial.print(sensorstring[i]);
        int value = atoi(sensorstring);
        Serial.println(value);
        }

   if (sensor_stringcomplete){                                                 //if a string from the Atlas Scientific product has been received in its entirety
       Serial.print(sensorstring);                                             //use the hardware serial port to send that data to the PC
       sensorstring = "";                                                      //clear the string:
       sensor_stringcomplete = false;                                          //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
      }
}



23  Using Arduino / Programming Questions / Re: Convert String From Serial Data to Numerical Value on: January 14, 2013, 06:13:41 pm
Thanks for that, I tried:

Code:
      int value = atoi(sensorstring);
      Serial.println(value);

Quote
error: cannot convert 'String' to 'const char*' for argument '1' to 'int atoi(const char*)'

Is it because it's reading sensorstring as '1'?
24  Using Arduino / Programming Questions / Convert String From Serial Data to Numerical Value on: January 14, 2013, 05:52:54 pm
Hello Everyone,
I've been able to read and select a string of data to display on my terminal using:

Code:
for (int i=6;i<11;i=i+1) {
  Serial.print(sensorstring[i]);
      }
Serial.println(" Liters/Minute");
//

My goal is to convert this value to a 'number' so I can run an algorithm on it. I tried to use

Code:
Serial.parseInt()

but am unable to figure out the syntax and the Arduino Reference site does not have any examples.

Any insight is appreciated.
25  Using Arduino / Programming Questions / Re: RS232 Program Not Printing Carriage Return in Serial Terminal or Minicom on: December 31, 2012, 01:34:53 pm
Hello,
I tried both suggestions and it still continues to print horizontally on the terminal

Code:
String inputstring = "";                                                       
String sensorstring = "";                                                     
boolean input_stringcomplete = false;                                         
boolean sensor_stringcomplete = false;                                         

  void setup(){                                                               
     Serial1.begin(38400);                                                     
     Serial3.begin(38400);                                                     
     inputstring.reserve(5);                                                   
     }
 
   void serialEvent() {                                                                     
               char inchar = (char)Serial1.read();                             
               inputstring += inchar;                                           
               if(inchar == '\n\r') {input_stringcomplete = true;}               
              } 

  void serialEvent3(){                                                         
              char inchar = (char)Serial3.read();                             
              sensorstring += inchar;                                         
              if(inchar == '\n\r') {sensor_stringcomplete = true;}               
             }

 void loop(){                                                                   
     
  if (input_stringcomplete){                                                   
      Serial1.println(inputstring);                                             
      inputstring = "";                                                       
      input_stringcomplete = false;                                           
      }

 if (sensor_stringcomplete){                                                   
      Serial3.println(sensorstring);                                             
      sensorstring = "";                                                       
      sensor_stringcomplete = false;                                           
      }
 }

I wonder if its my IDE settings.
26  Using Arduino / Programming Questions / RS232 Program Not Printing Carriage Return in Serial Terminal or Minicom on: December 31, 2012, 01:04:43 pm
Hello Everyone,
I'm trying to figure out why my serial terminal is not printing new lines in this program.

My Arduino IDE serial terminal prints sequentially ignoring the <CR> implemented in print()

Code:
String inputstring = "";                                                       
String sensorstring = "";                                                     
boolean input_stringcomplete = false;                                         
boolean sensor_stringcomplete = false;                                         

  void setup(){                                                               
     Serial1.begin(38400);                                                     
     Serial3.begin(38400);                                                     
     inputstring.reserve(5);                                                   
     }
 
   void serialEvent() {                                                                     
               char inchar = (char)Serial1.read();                             
               inputstring += inchar;                                           
               if(inchar == '\r') {input_stringcomplete = true;}               
              } 

  void serialEvent3(){                                                         
              char inchar = (char)Serial3.read();                             
              sensorstring += inchar;                                         
              if(inchar == '\r') {sensor_stringcomplete = true;}               
             }

 void loop(){                                                                   
     
  if (input_stringcomplete){                                                   
      Serial1.print(inputstring);                                             
      inputstring = "";                                                       
      input_stringcomplete = false;                                           
      }

 if (sensor_stringcomplete){                                                   
      Serial3.print(sensorstring);                                             
      sensorstring = "";                                                       
      sensor_stringcomplete = false;                                           
      }
 }



I was able to induce a new line being being generated in Minicom by using

Code:
Ctrl+A +a , (which is "adding a line feed")

Is there a way to do this in the code?

27  Using Arduino / Programming Questions / Use Serial Data to Write My Program on: December 29, 2012, 05:49:20 pm
I currently have successfully interfaced my Arduino with another Mcu which streams RS232 data in the following format:

e.g.

45.6723, 9.0876, 545.256<CR>

It comes in at a baud rate of 38400.

How do I use the second value "9.0876" in an "IF" or "While" >5 statement?
28  Using Arduino / Installation & Troubleshooting / Unable to Upload Program to Arduino Uno on: December 28, 2012, 03:07:26 pm
Just and FYI, if anyone is getting this error message:

"
avrdude: stk500_getsync(): not in sync: resp=0x00
"

Unplugging the Rx (Port 0/Rx and Port 1/Tx) in the Digital bank fixed my problem.
29  Using Arduino / Networking, Protocols, and Devices / Re: Connecting Two Microcontrollers Via Serial RS232 on: December 28, 2012, 01:47:59 am
OK, problem solved. I don't know what I did different this time around to make it work but here is what I changed

1. I have an Arduino Uno and an Arduino Mega 2560. The Mega seems to be working now (I see streaming data on the serial port)
2. I connected both grounds on the FLO-30 controller

30  Using Arduino / Networking, Protocols, and Devices / Re: Connecting Two Microcontrollers Via Serial RS232 on: December 27, 2012, 10:59:28 am
OK, it sounds like there is something funky going on with the manufacturer's code.

I guess I'll start something new using the Serial Library just to read the data stream coming in from the Atlas board. I'll post my results to conclude the thread.

Thanks for all your feedback.
Pages: 1 [2] 3