liquid crystal arduino

edited February 2017 in Arduino

hi guys. i have a problem with monitoring my x values by using lcd. My problem here is the x value shown in the processing and at the lcd is not tally.. here is my processing and arduino coding.

processing:

import processing.serial.*; Serial myPort; // Create object from Serial clas void setup() { size (800, 600); String portName = Serial.list()[0]; myPort = new Serial(this,"COM3", 9600); } void draw() { background(50); fill(150); stroke(255); ellipse(mouseX, mouseY, 40, 40); println(mouseX + " : " + mouseY); myPort.write(mouseX); printArray(Serial.list()); }

arduino:

#include <LiquidCrystal.h> LiquidCrystal lcd(12,11,5,4,3,2); char val; // Data received from the serial port int ledPin = 4; int incomingbyte = 0; // for incoming serial data void setup() { Serial.begin(9600); // opens serial port, sets data rate to 9600 bps lcd.begin(16,2); } void loop() { // send data only when you receive data: if (Serial.available() > 0) { // read the incoming byte: incomingbyte = Serial.read(); // say what you got: Serial.print("I received: "); Serial.println(incomingbyte, DEC); lcd.setCursor(0,0); lcd.print("x:"); lcd.setCursor(3,0); lcd.print(incomingbyte); lcd.setCursor(0,1); lcd.print(""); } } Capture lcd

Tagged:

Answers

  • the x value shown in the processing and at the lcd is not tally

    Your sketch and screen is showing your x value. What do you mean with a tally?

    Check this example: https://www.arduino.cc/en/Tutorial/Dimmer

    Notice your mouseX, an integer field, is converted to a byte as indicated in the previous post. A byte is limited to a range of 0 to 255. Your mouseX will reflect the width of your sketch. In your case, mouseX goes from 0 to 799.

    Check previous examples here: https://forum.processing.org/two/search?Search=readbytesuntil

    One last thing, to format your code in this forum, you select your code and hit ctrl+o. Ensure there is a line above and below your code block.

    Kf

  • @kfrajer the exact value on screen and the value on the lcd is different... the value should show from 0 to 800, but on my lcd it show different value than at the processing screen.

  • Change the size to screen to size(256,256); and see if that works as expected..

    Kf

  • @kfrajer no it doesnt work as expected. if im not mistaken it show ascii character on the lcd. wht if i want to change to string character. what code should i modify.

  • Try lcd.print(incomingbyte,DEC);

    Kf

  • @kfrajer still the same

  • I do not have a device to reproduce your observations. This is what I would do if I were. Be mindful, I didn't have the chance to test the code so you might have some simple errors, if any (Hopefully none fatal). My intention is to send known data from processing to arduino. I am sending numbers from 0 to 800, one every second. Arduino will receive the data, dsiplay what it received and send it back to Processing. Processing will intercept this data and it will print it out in the Processing's console.

    Kf

    Processing sketch

    //Sends numbers from 0 to width, one per second
    import processing.serial.*;
    
    Serial myPort;  
    int counter=0;
    String globalBuffer;
    
    void setup() {
      size (800, 600);
      globalBuffer=null;
      String portName = Serial.list()[0];
      myPort = new Serial(this, "COM3", 9600);
    }
    
    void draw() {
      background(50);
      fill(150);
      stroke(255);
    
      //Only sends every 60 frames or once per second
      if (frameCount%60==0) {
        myPort.write(counter);
        println("Send to  Arduino: "+nf(counter, 4)+"   0x"+hex(counter, 4));
        counter++;
    
        //Reset counter
        if (counter>width) counter=0;
      }
    
      processInputData(globalBuffer);
    }
    
    
    void serialEvent(int serial)
    {
      globalBuffer=myPort.readStringUntil('\n');
    }
    
    //Returns true when valid data is available
    boolean processInputData(String buff) {
    
      if (buff==null)
        return false;
    
      println("Received from arduino:\n===> "+buff);
    
      buff=null;  //Reset buffer for next data stream (IMPORTANT!!!)
      return true;
    }
    

    Arduino code

    #include <LiquidCrystal.h>
    LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 
    
    char val;                          // Data received from the serial port 
    int ledPin = 4; 
    int incomingbyte = 0;   // for incoming serial data
    
    void setup() {
      Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
      lcd.begin(16, 2);
    }
    void loop() {
      // send data only when you receive data:
      if (Serial.available() > 0) {
        // read the incoming byte:
        incomingbyte = Serial.read();
        // say what you got:
        Serial.print("I received: ");
        Serial.println(incomingbyte, DEC);
        lcd.setCursor(0, 0);
        lcd.print("x:");
        lcd.setCursor(3, 0);
        lcd.print(incomingbyte,DEC);
        lcd.setCursor(0, 1);
        lcd.print("");
        delay(50);
      }
    

    }

  • serial.read () arduino reads only the first byte, and parseInt () returns the first valid (long) integer number from the serial buffer. change:

    processing:
    myPort.write(mouseX+"\n");
    

    //
    arduino:

        void loop() {
          // send data only when you receive data:
          while (Serial.available() > 0) {
            // read the incoming byte:
            incomingbyte = Serial.parseInt();
            // say what you got:
            if (Serial.read() == '\n') {
              Serial.print("I received: ");
              Serial.println(incomingbyte, DEC);
              lcd.setCursor(0, 0);
              lcd.print("x:");
              lcd.setCursor(3, 0);
              lcd.print(incomingbyte);
              lcd.setCursor(0, 1);
            }
          }
          if (millis() % 1000 == 0) {
            lcd.clear();
          }
        }
    
Sign In or Register to comment.