4

I am trying to use a button switch in Arduino to trigger a visual display in Processing. I used "HIGH" and "LOW" to identify whether the button is pressed.

However, my code is constantly giving null instead of giving "HIGH" or "LOW" depending on the button state. I think this is pretty basic but I'm just quite lost. Any helps or comments would be appreciated!

Below is my code for Arduino and Processing respectively.

const int buttonPin = 2; 
const int LEDPin = 13; 

int buttonState = 0; 

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(LEDPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int analogValue = analogRead(A0)/4;
  Serial.write(analogValue);
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    Serial.write(HIGH);
    digitalWrite(LEDPin, HIGH);
  } else {
    Serial.write(LOW);
    digitalWrite(LEDPin, LOW);
  }
  delay(100);
}

Processing code:

import processing.serial.*;
Serial myPort;
String val; 

void setup() {
  size(400,400);
  String portName = Serial.list()[1];
  myPort = new Serial(this, portName, 9600);
}

void draw() {
  if (myPort.available() > 0) {
    val = myPort.readStringUntil('\n');
    println(val);
    if (val == "HIGH") {
      background(127,0,0);
    }
    if (val == "LOW") {
      background(144, 26, 251);
    }
  }
}
1
  • 2
    Serial.write(HIGH); doesn't send 'H','I','G','H' and '\n' but just a 1. Commented Nov 28, 2017 at 23:30

3 Answers 3

2

write()

Writes binary data to the serial port. This data is sent as a byte or series of bytes.

Serial.write(str) 

str: a string to send as a series of bytes


So when you use write HIGH and LOW in Serial.write, it will be send as a series of bytes. Edit your processing part to handle the incoming bytes. Just as follows :

import processing.serial.*;
Serial myPort;
String val; 
int len; //length of byte array

void setup() {
  size(400,400);
  String portName = Serial.list()[1];
  myPort = new Serial(this, portName, 9600);
}

void draw() {
  if ((len=myPort.available()) > 0) {
    for(i=0;i<len;i++)
      myByteArray=myPort.read();
    String val = String(myByteArray);
    println(val);
    if (val == "HIGH") {
      background(127,0,0);
    }
    if (val == "LOW") {
      background(144, 26, 251);
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is wrong. HIGH and LOW will be sent as a single byte 1 and 0. Also this isn't really a reliable way of reading a complete string.
2

just change your buttonState variable type from integer to boolean, and then use

if(buttonState) {
       Serial.println("HIGH");
}
else {
      Serial.println("LOW");
}

Comments

1

In Arduino, HIGH and LOW are defined as 1 and 0.

By executing Serial.write(HIGH); and Serial.write(LOW); you are just sending a single byte 1 or 0.

But according to your Processing code, you are expecting Serial.write(HIGH); to send 'H', 'I', 'G', 'H' and '\n' characters.

In your Arduino code, you need to replace Serial.write(HIGH); and Serial.write(LOW); with Serial.print("HIGH\n"); and Serial.print("LOW\n");.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.