Read Potentiometer/Serial from Arduino on processing

edited November 2017 in Arduino

Hey guys,

I'm working on a code which if written correctly should be able to print the value of my potentialmeter on processing. And then I dont mean with a flowing colour screen, that I can find on the internet. I want to print on the screen the number of the potentialmeter, and when I turn the wheel it, the number on processing should change.

I think my code does connect to the potentiometer and does print it on processing, but it keeps overwriting, the numbers dont change. It just literally writes over the previous numbers.

My arduino code :

void setup() {
 //initialize the serial communication:
 Serial.begin(9600); } 
void loop() {
 //send the value of analog input 0:
 Serial.println(analogRead(A0));
//wait a bit for the analog-to-digital converter to stabilize after last 
//reading: delay(2); } 

My processing code

 import processing.serial.*;
Serial myPort;
PFont myFont;
float val;
int[] pin_val;
int lf = 10;
boolean received;
float inByte = 0;

void setup() {      

size(325,400);

fill(51,153,255);


rect(10,10,300,150);

myFont = createFont("Arial", 18);

textFont(myFont, 18);

println(Serial.list());

myPort = new Serial (this, Serial.list()[0], 9600);

pin_val = new int[0];

val = 0;

received = false;

}

void draw()

{
textSize(30);

fill(255,0,0);

textAlign(CENTER);

textFont(myFont, 18);

text("received:   "+inByte, 160,50 );                                                             //This is where I believe my fault is.
fill(255,255,255);

ellipse(162.5,260,80,80);
}

void serialEvent (Serial myPort)
 {
String inString = myPort.readStringUntil('\n');

if (inString!= null) {

// trim off any whitespace;

inString = trim(inString);

//convert to an int and map to the screen height:

inbyte = float(inString);

println(inByte);

inByte = map(inByte, 0, 1023, 0, height);

}
}

I know there is a blue rectangle and a white ellips, but that's part of my homework.

Answers

  • edited November 2017 Answer ✓

    Please edit your post (gear icon) and fomat the code correctly by highlighting your code and pressing Ctrl+o (indent four spaces). Then people can actually read it, copy-paste to test, and help you.

    You aren't clearing the screen each frame, so you are drawing on top of your old text. Here is a wrong example:

    void draw() {
      text(frameCount, 50, 50);
    

    }

    Instead do this:

    void draw() {
      background(192);
      text(frameCount, 50, 50);
    

    }

    See background() in the reference: https://processing.org/reference/background_.html

  • edited November 2017

    I tried multiple times to get the code clear to read, but it won't work. it'll just appear between these things. nothing else

  • @jeremydouglas that solved it thankyou! But now I do have another question, the printed number has a maximum of 400, is this normal or should it also be able to print te max 1023 of the potentialmeter?

  • edited November 2017

    And in my comment it does work. Great.

  • edited November 2017

    ( please tag @jeremydouglass -- ss )

    Re:

    "the printed number has a maximum of 400, is this normal or should it also be able to print te max 1023 of the potentialmeter?"

    Look at your code -- I think, it is almost impossible to tell what is commented out and what isn't because you haven't indented each code line by at least four spaces and left an extra blank line above and below the code block.

    //convert to an int and map to the screen height:
    inByte = map(inByte, 0, 1023, 0, height);
    

    You received a number between 0 and 1023. Then you used map() to stretch that number to being between 0 and height. What is height?

  • edited November 2017

    @jeremydouglass SS!

    yes that height part is still of a previous part. In that piece the value of the potmeter was a sort of wave in processing. Since I lack of good arduino and processing skills, I coppied what I thought whas usefull and it mostly it works out quite well. So to be honest, I dont know what the use of height is .

  • edited November 2017

    @jeremydouglass since you have helped me very well, could you perhaps help me a little more? Because now I have to add a switch (button) to this programm. If you push the button the ellips has to turn green and else the button is just white.

    for Arduino code I used the StandarFirmata file which can be found in arduino. I read somewhere that was necessary.

    for proccesing code I have this

        import org.firmata.*;
    
        import processing.serial.*;
        import cc.arduino.*;
    
        Arduino arduino;
    
    
         int buttonPin = 2;
    
        Serial myPort;
    
        PFont myFont;
        float val;
        int[] pin_val;
        int lf = 10;
        boolean received;
        float inByte = 0;
        int buttonState = 0;
    
        void setup() {
          size(325,400);
           myFont = createFont("Arial", 18);
         textFont(myFont, 18);
    
         /*println(Serial.list());*/
    
         myPort = new Serial (this, Serial.list()[0], 9600);
         pin_val = new int[0];
         val = 0;
         received = false;
          arduino = new Arduino(this, Arduino.list()[0], 57600);
          arduino.pinMode(buttonPin, Arduino.INPUT);
    
        }
    
        void draw() {
    
          fill(51,153,255);
          rect(10,10,300,150);
          textSize(30);
          fill(0,0,0);
          textAlign(CENTER);
          textFont(myFont, 18);
    
          text("received:  " +inByte*2.5575, 160,50 );
    
         }
    
    
        /* fill(255,255,255);
          ellipse(162.5,260,80,80);*/
    
    
        void serialEvent (Serial myPort) {
             String inString = myPort.readStringUntil('\n');
              if (inString != null) {
              // trim off any whitespace:
              inString = trim(inString);
              // convert to an int and map to the screen height:
              inByte = float(inString);
              println(inByte);
              inByte = map(inByte, 0, 1023, 0, height);}
            }
    
    
        void arduinoEvent(Arduino arduino){ //I believe this is not entirely correct either
    
         if (arduino.digitalRead(buttonPin) == Arduino.LOW)  {
              fill(255,255,255);
          ellipse(162.5,260,80,80);
         }
         else {
              fill(0,255,0);
            ellipse(162.5,260,80,80);
          }
        } 
    

    The program won't run because it says the port is busy, I get that because at the top I connected 'Arduino' also to port 0. But I dont know how to do it otherwise, since both my button and my potmeter are connected to that port.

  • Please format your code. Edit your post (gear on top right side of any of your posts), select your code and hit ctrl+o. Leave an empty line above and below your block of code. Details here: https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    Please edit your post... It is really hard to read your code. Unfortunately your previous code doesn't seem to be properly formatted either.

    Your button and potentiometer are connected to the same arduino, right? Then you need to open the port only once. You are opening the same port twice (and using different speeds). Both are a big no no.

    In your case, I will suggest you write a small program that works with a push button first. THen write another program that uses the potentiometer. Finally, merge the two programs into a third program. This exercise will help you understand the software and routines you need to use with each of your hardware components. It also helps by making sure each component is working by itself. This will save lots of debugging... specially because we don't have your hardware to run your code and test it.

    Kf

  • @DeDaymar -- I'm glad it was helpful.

    People have asked you several times to edit and correctly format the code in your posts, and given you instructions on how to do so. Please, please do so if you would like further help from me on this forum.

  • @kfrajer @jeremydouglass okay so I finally got it. Sorry it took so long

  • @kfrajer

    In your case, I will suggest you write a small program that works with a push button first. THen write another program that uses the potentiometer. Finally, merge the two programs into a third program.

    I did, that's how I figured out what arduino program to use. And I understand that it's a no go to connect both the button and the potentiometer to the same port, but the button has to be connected as well..

  • edited November 2017

    Guys I figured it out. My code works! I used an example from processing called arduino_input. I changed that code to how I needed it to be and now it works perfectly. Both my button and my potentialmeter do what I want.

        import processing.serial.*;
    
        import cc.arduino.*;
    
        Arduino arduino;
    
        void setup() {
        size(325,400);
    
        println(Arduino.list());
    
        arduino = new Arduino(this, Arduino.list()[0], 57600);
    
        for (int i = 0; i <= 13; i++)
        arduino.pinMode(i, Arduino.INPUT);
        }
    
        void draw() {
        for (int i = 0; i <1; i++) {
        if (arduino.digitalRead(2) == Arduino.HIGH)
        fill(0,255,0);
        else
        fill(255,255,255);
    
        ellipse(162.5 - i * 30, 260, 80, 80);
          }
    
         for (int i = 0; i <= 5; i++) {
         fill(51,153,255);
         rect(10,10,300,150);
         textSize(30);
         fill(0,0,0);
         textAlign(CENTER);
         text("received:   "+arduino.analogRead(0), 160,50);}}
    

    So thank you very much for your help, both. I do have more stuff I have to make, so I might be back but for now I am done.

  • Congratulations, @DeDaymar! Thanks also for sharing your solution with the forum.

  • Great to hear it is working.

    it's a no go to connect both the button and the potentiometer to the same port

    Just to clarify, I was referring no to open the same port of your computer twice. If you issue a second open command on an already opened port, you will get an error of "port already open" or along those lines.

    Kf

Sign In or Register to comment.