Go Down

Topic: arduino and processing cummunicate serial (Read 274 times) previous topic - next topic

SeventhSky

hello i'm a newbie...any one can help me i wanna make water level sensor using srf04 and arduino uno...but i have a problem in processing program...i wanna read output from sensor and send data from processing to arduino to limit the water level.... thanks  :smiley: 

PaulS

Quote
i wanna read output from sensor
It would be better to have the Arduino do that.

Quote
and send data from processing to arduino
What data?

Quote
to limit the water level
A ping type sensor can not limit the water level. Shutting off a valve, maybe...

SeventhSky

It would be better to have the Arduino do that.
What data?
A ping type sensor can not limit the water level. Shutting off a valve, maybe...
yeah i wanna limit the water level or turn off the valve... but i have a problem with processing program...
i can't send float or int value... for example like 1.5 or 2.0 or 3.2 an etc
do you have a suggestion?? thanks before :D

SeventhSky


PaulS

Quote
i can't send float or int value.
Why not? Everyone else can. Where IS your code?

SeventhSky

Why not? Everyone else can. Where IS your code?
my arduino code :
 char val; // Data received from the serial port
 int ledPin = 4; // Set the pin to digital I/O 4
 int ledPin0 = 7;
 void setup() {
 pinMode(ledPin, OUTPUT);
 pinMode(ledPin0, OUTPUT); // Set pin as OUTPUT
 Serial.begin(9600); // Start serial communication at 9600 bps
 }
 
 void loop() {
 while (Serial.available()) { // If data is available to read,
 val = Serial.read(); // read it and store it in val
 }
 if (val == '1') { // If H was received
 digitalWrite(ledPin, LOW);
 digitalWrite(ledPin0, LOW);// turn the LED on
 Serial.print("hurung");
 Serial.print("\n");
 } else {
 digitalWrite(ledPin, HIGH);
 digitalWrite(ledPin0, HIGH); // Otherwise turn it OFF
 //Serial.print("parem");
 Serial.print(val);
 Serial.print("\n");
 }
 delay(100); // Wait 100 milliseconds for next reading
 }
 
and the processing code :

import processing.serial.*;
ButtonA ok_button;// the button
ButtonB reset_button;
int clk = 1;       // number of times the button is clicked
Serial myPort;  // Create object from Serial class
String val;        // Data received from the serial port
String a=" ";

void setup() {
  size (600, 350);
  smooth();
   // I know that the first port in the serial list on my mac
  // is always my  FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
  myPort.bufferUntil('\n');
  // create the button object
  ok_button = new ButtonA("OK", 20, 20, 100, 50);
  reset_button = new ButtonB("Reset", 20, 80, 100, 50);
}

void draw() {
  background(0);
  textSize(14);
  fill(255, 255, 255, 255);
  textAlign(LEFT);
  text("Masukan Daya Tampung = "+ a + " Liter", 20, 160);//untuk mengatur koordinat text
  text(val ,20,180);
  // draw a square if the mouse curser is over the button
  if (ok_button.MouseIsOver()) {
    rect(20, 20, 100, 50);
                }
  else if (reset_button.MouseIsOver()){
    rect(20, 80, 100, 50);
                }
  else{
  // hide the square if the mouse cursor is not over the button
                }
  // draw the button in the window
  ok_button.Draw();
  reset_button.Draw();
  }

void serialEvent (Serial myPort){
 val = myPort.readStringUntil('\n');
  if(val != null){
   val=trim(val);
  }
    }

void keyPressed(){ 
  if( key >= '0' && key <= '9' ){
     a+=str(key);
    }
   }
//perintah if untuk mengecek apakah tombol di tekan atau tidak
void mousePressed()
{
  if (ok_button.MouseIsOver()) {
    // perintah untuk mencetak jika tombol di klik
    myPort.write(a);
    //myPort.write("\n");
    a=" ";
   
  }
   else if (reset_button.MouseIsOver()) {// If mouse is not over square,
    myPort.write('H');        // send an L otherwise
    //print("no click");
    a=" ";
  }
}

// the Button class
class ButtonA {
  String label; // label tombol
  float x;      // kordinat x tombol
  float y;      // top left corner y position
  float w;      // variable untuk lebar tobol
  float h;      // variable untuk tinggi tombol
 
  // constructor
  ButtonA(String labelA, float xpos, float ypos, float widthA, float heightA) {
    label = labelA;
    x = xpos;
    y = ypos;
    w = widthA;
    h = heightA;
  }
  void Draw() {
    fill(218);
    stroke(141);
    rect(x, y, w, h, 10);
    textAlign(CENTER, CENTER);
    fill(0);
    text(label, x + (w / 2), y + (h / 2));
  }
 
  boolean MouseIsOver() {
    if (mouseX > x && mouseX < (x + w) && mouseY > y && mouseY < (y + h)) {
      return true;
    }
    return false;
  }
}

class ButtonB {
  String label; // label tombol
  float x;      // top left corner x position
  float y;      // top left corner y position
  float w;      // variable untuk lebar tobol
  float h;      // variable untuk tinggi tombol
 
  // constructor
  ButtonB(String labelB, float xpos, float ypos, float widthB, float heightB) {
    label = labelB;
    x = xpos;
    y = ypos;
    w = widthB;
    h = heightB;
  }
  void Draw() {
    fill(218);
    stroke(141);
    rect(x, y, w, h, 10);
    textAlign(CENTER, CENTER);
    fill(0);
    text(label, x + (w / 2), y + (h / 2));
  }
 
  boolean MouseIsOver() {
    if (mouseX > x && mouseX < (x + w) && mouseY > y && mouseY < (y + h)) {
      return true;
    }
    return false;
  }
}


first I try to make the led on before I try ultrasonic sensor... if i type 1 char the led can turn on... but if I type 2 char in processing it not work... I very confused...confuse about send serial from processing to arduino with 2 digit number ..
for example : 20 or 10 and etc...
thanks before my friend...sori if my grammer so bad... :)

PaulS

Code: [Select]
while (Serial.available()) { // If data is available to read,
 val = Serial.read(); // read it and store it in val
 }

If Processing sends "20", what will be in val, when the while loop ends?

SeventhSky

Code: [Select]
while (Serial.available()) { // If data is available to read,
 val = Serial.read(); // read it and store it in val
 }

If Processing sends "20", what will be in val, when the while loop ends?
big thanks very very much paul.... my problem is solved... thank :D

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy