Sending over Arduino analog values to Processing doesn't work

Hi all,

For a schoolassignment i’am creating a 3D boxchart from a CSV file which represents COVID-19 cases in the Netherlands. The whole code is working fine for me as it is. At this moment i’ve got three buttons, a LED and a potentiometer plugged into my Arduino. The buttons and LED work fine and those values get send over between arduino and Processing without any problems. Except the potentiometer analog values are not recived by Processing. When printing the values in Arduino they work properly but when i change the Serial.println to Serial.write the only thing Processing recives is a 0. Hopefully anybody here can help me to send over the analog values.

ARDUINO CODE:

int sensorPin = A1;
int sensorVal;
int doorStuur;

int leftButton = 2;
int rightButton = 3;
int draaiButton = 5;
int ledPin = 9;

boolean idle = false;

int val;

int old_sensorVal;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(leftButton, INPUT);
  pinMode(rightButton, INPUT);
  pinMode(draaiButton, INPUT);
}

void loop() {

  int sensorVal = map(analogRead(sensorPin), 0, 1023, 0, 250);
  if (sensorVal < old_sensorVal * 0.9 || sensorVal > old_sensorVal * 1.1 ) {
    old_sensorVal = sensorVal;  // save the changed value
    sensorVal = doorStuur;
    Serial.write(doorStuur);
    delay(10);
  }

  if (digitalRead(leftButton) == HIGH && idle == false) {  // If switch is ON,
    Serial.write(252);               // send 1 to Processing
    delay(10);
    idle = true;
  }

  if (digitalRead(rightButton) == HIGH && idle == false) {  // If switch is ON,
    Serial.write(253);               // send 1 to Processing
    delay(10);
    idle = true;
  }

  if (digitalRead(draaiButton) == HIGH && idle == false) {
    Serial.write(254);
    delay(10);
    idle = true;
  }

  if (idle == true) {
    Serial.write(0);
    delay(10);
    idle = false;
  }

  //vanaf hier verwacht ik data terug
  while (Serial.available()) {
    val = Serial.read(); // altijd tussen 0 - 255
  }

  if (val == 'H') {
    analogWrite(ledPin, HIGH);
  } else {

    analogWrite(ledPin, LOW);
  }


  delay(100);
}

PROCESSING CODE:

import processing.serial.*;

boolean bewegenHorizon = true;
Table data;
int rijen;
int minWaarde = 2;
int maxWaarde = 2299;
int minWaardeDood = 1;
int maxWaardeDood = 329;
int boxGrootte;
float meldingWaarde;
float dodenWaarde;
int draaien = 0;
float afstand;
boolean eersteKeer = true;
int potmeterVal;
int counter = 0;
int analogeWaarde;
boolean eersteDruk = true;
String gemeente;

int beweging = width/2;

float r, g, b;


Serial myPort;  // Create object from Serial class
int arduinoVal;      // Data received from the serial port


void setup() {
  data = loadTable("covid.csv", "header");
  rijen = data.getRowCount();
  size(800, 600, P3D);
  printArray(Serial.list());
  String portName = Serial.list()[1];
  myPort = new Serial(this, portName, 9600);
  colorMode(RGB);

  textAlign(CENTER);

  boxGrootte = 30;
}

void draw() {
  if ( myPort.available() > 0) {  // If data is available,
    arduinoVal = myPort.read();         // read it and store it in val
    println(arduinoVal);
  }



  if (bewegenHorizon == true) {
    translate(beweging, height/2, mouseY*2);
    myPort.write('L');
  } else {
    translate(width/2, height/2, beweging);
    myPort.write('H');
  }


  if (arduinoVal == 252) {
    beweging = beweging+20;
  }
  if (arduinoVal == 253) {
    beweging = beweging-20;
  }



  background(255);
  lights();
  noStroke();

  if (eersteDruk == true) {
    if (arduinoVal == 254) {
      delay (500);
      draaien = 90;
      bewegenHorizon = false;
      delay (500);
      eersteDruk = false;
    }
  } else if (arduinoVal == 254) {
    delay (500);
    draaien = 0;
    bewegenHorizon = true;
    delay (500);
    eersteDruk = true;
  }


  if (arduinoVal == 255) {
    draaien = 0;
    bewegenHorizon = true;
  }


  rotateY(radians(draaien));


  for (int i = 0; i < rijen; i++) {

    meldingWaarde = map(data.getInt(i, "Meldingen"), minWaarde, maxWaarde, 0, height);
    dodenWaarde = map(data.getInt(i, "Overleden"), minWaardeDood, maxWaardeDood, 0, 255);
    gemeente = data.getString(i, "Category");

    r = dodenWaarde;
    g = 0;
    b = 0;
    fill(r, g, b);

    pushMatrix();
    translate((i*boxGrootte*5) - (width/2), 0);
    box(boxGrootte, meldingWaarde, dodenWaarde);
    popMatrix();

    if (bewegenHorizon == true) {
      pushMatrix();
      translate((i*boxGrootte*5) - (width/2), 0);
      textSize(10);
      fill(g, b, 255);
      text(gemeente, 0, 100);
      popMatrix();
    } else {
      pushMatrix();
      translate((i*boxGrootte*5) - (width/2), 0);
      rotateY(radians(270));
      textSize(3);
      fill(g, b, 255);
      text(gemeente, 0, 0, boxGrootte + 5);
      popMatrix();
    }
  }
}

Hopefully someone here can help me with fixing this problem. If any more info is needed i’am ofcourse willing to help as soon as possible! Thanks in advance!

1 Like

Hi @Monfan123,

I think your code is confused and you have not set the value in doorStuur. Perhaps the line ‘sensorVal = doorStuur;’ was meant to be the other way round.

I think your comms scheme is that you are sending single byte values, 0 to 250 is allocated for the analogue range and pairs of values 251…255 are for digital states. That’s an interesting way of getting the most functionality without using multi-byte values and start and end markers.

1 Like

Wow, totally overlooked that like a thousand times. It does work like a charm now. Some fresh eyes on your project definitely help sometimes. Thankyou!

And indeed i try sticking to one byte at a time so the 0 - 250 are for analouge and the 5 left over values are for my digital inputs. This because i began by connecting the potentiometer.

Thanks again!

1 Like