sending 3 digit number from Processing to Arduino

Dear all

I have been trying my best to solve it, but I ran out of ideas!

Basically, in void draw() of my Processing, I read a text file which contains a 3 digit number, and write this number to the serial port.

void draw() {
 
  String data1[] = loadStrings("http://arduinoweb.net46.net/Piezo/PitchCommand.txt"); 
 
  int i = parseInt(data1[0]);
  
  port.write(i);  
  port.write(','); //delimiter
  delay (1000);
}

Let’s assume that PitchCommand.txt contains only a 3-digit number, 262.
Since void draw() will loop indefinitely, in my mind, the serial buffer will contain 262,262,262,262, … (on and on)

In my Arduino sketch , I have the following code:

void loop() {
   /*  check if data has been sent from the computer: */

 while (Serial.available()) {
    /* read the most recent byte */
    byteRead = Serial.read();
    
    //listen for numbers between 0-9
    if(byteRead>47 && byteRead<58){
         num1=(num1*10)+(byteRead-48); //reconstruct the 3-digit number
    }
    
    //Listen for a comma sign (byte code 44) 

    if(byteRead==44){
         pitch = num1; //used to play a tone 
         num1 = 0; //reset num1

        tone(8, pitch);//piezo connected to pin 8
        delay (1000);
        noTone(8);

    }
    
   
  }

 }

The idea is, I have a php file that writes into PitchCommand.txt which will change the pitch of the piezo via Processing…

the problem is, these codes are not working!

Any idea how I can fix it?

thanks a lot !!!

follow up..

I tested the arduino sketch independently,using the serial monitor. for instance, when I typed in 262,345,

I got the correct numbers assigned to the pitch variable. (the piezo played the correct pitch)

Which means, the problem must lie in Processing code when writing to the serial port......

Tip - you can use chars and not ascii codes eg

    if(byteRead>47 && byteRead<58){
      num1=(num1*10)+(byteRead-48); //reconstruct the 3-digit number
    if(byteRead='0' && byteRead<='9'){
      num1=(num1*10)+(byteRead-'0'); //reconstruct the 3-digit number

Mark

Thanks Mark for the tip.

I think my Arduino Sketch is correct. I wonder what is wrong with the Processing sketch....?

What's it reading from the txt file? Is it even finding it? Add some debug prints to it!

Mark

Hi Mark,

I am positive it is reading the right information from the txt file.

I finally solved it! Have been battling with this since yesterday!!!

I replaced the code in processing to the following:

  String data1[] = loadStrings("http://arduinoweb.net46.net/Piezo/PitchCommand.txt"); // Insert the location of your .txt file

 //somehow writing integer to serial port does not work!!
  //int i = parseInt(data1[0]);
  
  port.write(data1[0]);  // Send "L" over serial to set LED to LOW
  port.write(','); //delimiter

In the earlier Processing sketch, I converted the string to integer before writing to the port and it did not work!

Now I just write the string as it is into the serial port, and it works!

Any reason why???

Any reason why???

An int is storing the number in binary not as separate characters '2' '6' '2'. Your Arduino code is wanting to see separate characters.

radman:
An int is storing the number in binary not as separate characters ‘2’ ‘6’ ‘2’.
Your Arduino code is wanting to see separate characters.

Ah I see! If I were to store the 3-digit number in binary, how could I convert this binary back to 3-digit number in Arduino?

To avoid confusion you need to take care how you talk about numbers. Your question would be better phrased something like this; If I were to store a three digit decimal number in binary how could I convert this binary into three ASCII characters representing the decimal number?

Converting numbers to and from characters and strings is a common thing to do so functions are available for doing this, but here is essentially what is going on;

To keep things simple lets use an unsigned byte (8 bits) to store a three digit decimal number in binary. With 8 bits (unsigned) you can store numbers in the range 0-255 decimal (to store larger numbers you need more bytes) 00000000 bin = 0 dec 11111111 bin = 255 dec so 01111011 bin = 123 dec

How do you convert the binary representation of decimal 123 into the threee ASCII characters '1', '2', '3'. Well if you repeatedely take the remainder upon division by 10 you get

00000011 bin = 3 (123/10 = 12 remainder 3 ) 00000010 bin = 2 ( 12/10 = 1 remainder 2 ) 00000001 bin = 1 ( 1/10 = 0 remainder 1 )

But 00000000 bin is not the ASCII character '0' that is 00110000 bin or 48 dec. so add 48 dec to the remainders above to convert to ASCII characters.

3 + 48 = 51 dec = 00110011 bin = '3' ASCII

Never looked at processing but port.print(i) is the one I would have tried.

Mark

May not be what you are looking for, but code that takes multi character input from the serial monitor and converts the characters into a number for servo control.

//zoomkat 3-5-12 simple delimited ',' string parce 
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

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

  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-delomit-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like 700, or 1500, or 2000,
  //or like 30, or 90, or 180,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          myservo.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          myservo.write(n);
        }

        //do stuff with the captured readString 
        readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}