Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: if looping in parsing string data on: May 13, 2014, 09:26:12 pm
Thank Nick Gammon, PaulS and Robin2

the link that Nick gave is very helpful, I will use this the method, but for a moment I am using this code an working but not to well.

the different just moving the
Code:
delay(5)
to the  place before
Code:
while (Serial.available()) {

Here is the code:
Code:
#include <Servo.h>
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo
Servo myservo2;
int result;

void setup() {
  Serial.begin(9600);
  myservo1.attach(8);  //the pin for the servo control
  myservo2.attach(7);
 Serial.println("test"); // so I can keep track of what is loaded
}


void abc () {
   
   while (Serial.available()) {
   
    delay(5);  //delay to allow buffer to fill
   
   if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    }
  }
   
  if (readString.length() >0) {
      Serial.println(readString); //see what was received
     
      servo1 = readString.substring(0, 3); //get the first four characters
      servo2 = readString.substring(3, 6); //get the next four characters
     
   
      int n1 = servo1.toInt();
      int n2 = servo2.toInt();

      Serial.println("the numbers are :");
      Serial.println(n1);  //print to serial monitor to see number results
      Serial.println(n2);
       
        myservo1.write(n1);   
        myservo2.write(n2);   
   
      readString="";
      servo1="";
      servo2="";
  }
}
   



void loop() {

if(Serial.available()){
    result =  Serial.read();
  if (result== 'g'){
    Serial.println("xxx");
    delay(30); 
  abc();

  }
}

}
 

but I will try the State Machine methods anyway.
2  Using Arduino / Programming Questions / Re: if looping in parsing string data on: May 12, 2014, 11:30:15 pm
Thank you


I have try my code and when I press 3, the program print xxx, so the problem is not in the

Code:
if (result== '3'){

I have tried with serial monitor, I input 3 then showed xxx in the monitor
and I input 6 data say 060120 and the monitor did not respond
3  Using Arduino / Programming Questions / if looping in parsing string data on: May 12, 2014, 10:43:00 pm
Hi all,

I want to control robot via PC serial port with arduino mega,
I send "a" to pivot to the left, "d" to the right etc
all the program is working good.

And I want to add some program to control 2 servos to move the camera pan and tilt according to the headset,
I use program from  http://forum.arduino.cc/index.php/topic,165050.0.html and it works.

I want to send character "3" before accessing servos program, but it is not working.

here my code

Code:

#include <Servo.h>
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo
Servo myservo2;
int result;

void setup() {
  Serial.begin(9600);
  myservo1.attach(8);  //the pin for the servo control
  myservo2.attach(7);
 Serial.println("test"); // to keep track of what is loaded
}

void loop() {

if(Serial.available()){
  result =  Serial.read();
  if (result== '3'){
    
      while (Serial.available()) {
  
  
    
            Serial.println("xxx"); //just make sure that the program have entered the if loop
            delay(5);  //delay to allow buffer to fill
  
             if (Serial.available() >0) {
                  char c = Serial.read();  //gets one byte from serial buffer
                   readString += c; //makes the string readString
                      }
                  }
     
               if (readString.length() >0) {
                Serial.println(readString); //see what was received
           
                servo1 = readString.substring(0, 3); //get the first three characters
                servo2 = readString.substring(3, 6); //get the next three characters
      
                int n1 = servo1.toInt();
                int n2 = servo2.toInt();

                  Serial.println("the numbers are :");
                  Serial.println(n1);  //print to serial monitor to see number results
                  Serial.println(n2);
        
                   myservo1.write(n1);    
                   myservo2.write(n2);    
      
                    readString="";
                    servo1="";
                    servo2="";
                         }
                }
   }
}

I use serial monitor, and the the program is fail to read the 6 character for moving the servos.

Do I make something wrong with the code?

4  Using Arduino / Interfacing w/ Software on the Computer / Re: interfacing arduino mega adk with vc++ on: January 05, 2013, 09:18:28 pm
Thank you,

I understand what you mean, I have to send character as marker that is the end of the data.

But for implementation in arduino and c++ coding I do not understand, I am newbie in programming.
5  Using Arduino / Interfacing w/ Software on the Computer / Re: interfacing arduino mega adk with vc++ on: January 05, 2013, 09:03:10 pm
Thanks for the quick answers,
for:
Code:
char buf0[1000], buf1[1000], buf2[1000], buf3[1000],buf4[1000], buf5[1000],buf6[1000], buf7[1000], buf8[1000], buf9[1000],buf10[1000], buf11[1000],buf12[1000], buf13[1000],buf14[1000], buf15[1000];
yes I was to afraid that the buffer will not enough  smiley-red

so do you recommend to change 
 
Code:
Serial.print("\n\r");
with
Code:
Serial.println()

and
for more than 9 the
Code:
char request[1];
with
[code]char request[2];

but when I try with the serial monitor in arduino is fine I write 10 and the result is fine. but not working with the vc++

actually for 10, 11, etc I use program:

Code:
sprintf_s(buffer,"10");   
    oSerial.WriteData(buffer,2);

do I right?[/code]
6  Using Arduino / Interfacing w/ Software on the Computer / Re: interfacing arduino mega adk with vc++ on: January 05, 2013, 08:40:44 pm
If I use 9 sensors/input the output is fine, but when more than 9 say 10 sensors the error is begin.  smiley-sad-blue
7  Using Arduino / Interfacing w/ Software on the Computer / interfacing arduino mega adk with vc++ on: January 05, 2013, 08:23:38 pm
Hi,

I want to read analog sensor using arduino mega adk and with Visual Studio C++

but the result is not correct, look like in the attachment:
sensor 0:023
1
sensor1:023
1
sensor2:023
9
sensor3:21

etc

my arduino program is:
Code:
void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  char request[1];
  if(Serial.available()){
    request[0]=Serial.read();
    Serial.print(analogRead(atoi(request)));
    Serial.print("\n\r");
  };
  delay(2);                     
}

and my vc++ program is:
Code:
#include <windows.h>
#include "stdafx.h"
#include "SerialClass.h"

char buffer[20];
char buf0[1000], buf1[1000], buf2[1000], buf3[1000],buf4[1000], buf5[1000],buf6[1000], buf7[1000], buf8[1000], buf9[1000],buf10[1000], buf11[1000],buf12[1000], buf13[1000],buf14[1000], buf15[1000];


int _tmain(int argc, _TCHAR* argv[])
{
   Serial oSerial("COM5:");//arduino uno com 6
   
   while(1){
sprintf_s(buffer,"0");   
    oSerial.WriteData(buffer,1);
Sleep(100);
oSerial.ReadData(buf0,6);
printf("Sensor 0: %s \n",buf0);
Sleep(100);


   sprintf_s(buffer,"1");   
    oSerial.WriteData(buffer,1);
Sleep(100);
oSerial.ReadData(buf1,6);
printf("Sensor 1: %s\n",buf1);
Sleep(100);

sprintf_s(buffer,"2");   
    oSerial.WriteData(buffer,1);
Sleep(100);
oSerial.ReadData(buf2,6);
printf("Sensor 2: %s \n",buf2);
Sleep(100);


   sprintf_s(buffer,"3");   
    oSerial.WriteData(buffer,1);
Sleep(100);
oSerial.ReadData(buf3,6);
printf("Sensor 3: %s\n",buf3);
Sleep(100);

   sprintf_s(buffer,"4");   
    oSerial.WriteData(buffer,1);
Sleep(100);
oSerial.ReadData(buf4,6);
printf("Sensor 4: %s \n",buf4);
Sleep(100);


   sprintf_s(buffer,"5");   
    oSerial.WriteData(buffer,1);
Sleep(100);
oSerial.ReadData(buf5,6);
printf("Sensor 5: %s\n",buf5);
Sleep(100);

sprintf_s(buffer,"6");   
    oSerial.WriteData(buffer,1);
Sleep(100);
oSerial.ReadData(buf6,6);
printf("Sensor 6: %s \n",buf6);
Sleep(100);


   sprintf_s(buffer,"7");   
    oSerial.WriteData(buffer,1);
Sleep(100);
oSerial.ReadData(buf7,6);
printf("Sensor 7: %s\n",buf7);
Sleep(100);
   
    sprintf_s(buffer,"8");   
    oSerial.WriteData(buffer,1);
Sleep(100);
oSerial.ReadData(buf8,6);
printf("Sensor 8: %s \n",buf8);
Sleep(100);


   sprintf_s(buffer,"9");   
    oSerial.WriteData(buffer,1);
Sleep(100);
oSerial.ReadData(buf9,6);
printf("Sensor 9: %s\n",buf9);
Sleep(100);

sprintf_s(buffer,"10");   
    oSerial.WriteData(buffer,2);
Sleep(100);
oSerial.ReadData(buf10,6);
printf("Sensor 10: %s \n",buf10);
Sleep(100);


   sprintf_s(buffer,"11");   
    oSerial.WriteData(buffer,2);
Sleep(100);
oSerial.ReadData(buf11,6);
printf("Sensor 11: %s\n",buf11);
Sleep(100);

   sprintf_s(buffer,"12");   
    oSerial.WriteData(buffer,2);
Sleep(100);
oSerial.ReadData(buf12,6);
printf("Sensor 12 %s \n",buf12);
Sleep(100);


   sprintf_s(buffer,"13");   
    oSerial.WriteData(buffer,2);
Sleep(100);
oSerial.ReadData(buf13,6);
printf("Sensor 13: %s\n",buf13);
Sleep(100);

sprintf_s(buffer,"14");   
    oSerial.WriteData(buffer,2);
Sleep(100);
oSerial.ReadData(buf14,6);
printf("Sensor 14: %s \n",buf14);
Sleep(100);


   sprintf_s(buffer,"15");   
    oSerial.WriteData(buffer,2);
Sleep(100);
oSerial.ReadData(buf15,6);
printf("Sensor 15: %s\n",buf15);
Sleep(100);   
   }
}
8  Using Arduino / Interfacing w/ Software on the Computer / Re: programming serial arduino with visual C++ on: December 27, 2012, 09:29:05 pm
Thank you Pauls, I are right after I change to :
Code:
oSerial.ReadData(buf0,6);

it seems all right for data less than 4 digit
after 4 digit let say 1023 the display is

sensor 0:1023
(here there are 18 squares)

what the meaning of those squares?

9  Using Arduino / Interfacing w/ Software on the Computer / programming serial arduino with visual C++ on: December 27, 2012, 06:49:16 pm
Hi all
I try to read sensor analog from serial using Visual studio C++ 2010.
my arduino program :

Code:
void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  char request[1];
  if(Serial.available()){
    request[0]=Serial.read();
    Serial.print(analogRead(atoi(request)));
    Serial.print("\n\r");
  };
  delay(2);                     
}

my Serial C++ program :

Code:
#include <windows.h>
#include "stdafx.h"
#include "SerialClass.h"

char buffer[20];
char buf0[200];


int _tmain(int argc, _TCHAR* argv[])
{
   Serial oSerial("COM6:");
   
   while(1){
sprintf_s(buffer,"0");   
        oSerial.WriteData(buffer,1);
Sleep(1000);
oSerial.ReadData(buf0,4);
printf("Sensor 0: %s \n",buf0);
Sleep(1000); }
}

I put 5V on sensor 0 but the result is not stable:
Code:
sensor 0 :
sensor 0 : 10230
sensor 0:
100
sensor 0: 23
0
sensor 0: 10230

and so on,

I think there is no wrong with arduino side because when I use serial monitor on arduino it seems alright.
Any suggestion?
10  Using Arduino / Interfacing w/ Software on the Computer / Re: read analog sensor with arduino uno and programming C on: November 25, 2012, 11:34:02 pm
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)



11  Using Arduino / Interfacing w/ Software on the Computer / Re: read analog sensor with arduino uno and programming C on: November 25, 2012, 06:59:50 pm
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
12  Using Arduino / Interfacing w/ Software on the Computer / read analog sensor with arduino uno and programming C on: November 24, 2012, 10:43:28 pm
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.
Pages: [1]