Quantcast
Channel: Arduino - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 747

Send Data from Arduino to Processing over serial // help please

$
0
0

I am trying to send data from an Arduino Uno to Processing.

Video of situation: image

I made a simple sketch which does this no problem. It is only when I try to have the Arduino send data to Procesing in an "if conditional" from an analog sensor reading that I run into trouble.

For instance, the simple send from Arduino looks like this:

int ledPin = 13;

void setup()
{
  Serial.begin(9600);
   pinMode(ledPin, OUTPUT);
}
void loop()
{
  Serial.write(1);
   digitalWrite(ledPin, HIGH);
  delay(200);
  Serial.write(0);
   digitalWrite(ledPin, LOW);
  delay(200);
}

And the corresponding Processing code looks like this:

import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;

void setup() {
  //change the 0 to a 1 or 2 etc. to match your port
  String portName = Serial.list()[2]; 
  myPort = new Serial(this, portName, 9600);

  rectMode(CENTER);
}

void draw()
{
  background(0);
  fill(255);

  if (myPort.available()>0)
  {
    val = myPort.read();
  }
  println(val);

  if (val == 1)  {
    rect(width/2, height/2, 30, 30);
  }
}

It makes a white square appears when the number "1" is sent to Processing. It works so simple. It works every time. I was so pleased.

However, when I try it with the next Arduino sketch, nothing ever happens. It always stays on "0" even though I can see on the Arduino the LED turns on showing that yes, this piece of code has run. The Processing portion can stay exactly the same.

Why am I not seeing it work right? I have tried so many things. This tells me it's either painfully obvious or I've done something horribly karmically wrong and this is my punishment.

Thank you for nay tips.

    // Used to interface with electronic drum pads
    // Using a piezo sensor
    // Casey Scalf 2015


    int ledPin = 13;
    int padPin = 0; // Red
    // Black is Ground

    byte val;
    byte a = 0;

    int THRESHOLD = 20;

    void setup() {

      Serial.begin(9600);

      pinMode(ledPin, OUTPUT);
    }

    void loop() {

      val = analogRead(padPin);

      if (val <= THRESHOLD) {
        digitalWrite(ledPin, LOW);
        a = 0;
        Serial.write(a);
      }

        if (val >= THRESHOLD) {
        digitalWrite(ledPin, HIGH);
        a = 1;
        Serial.write(a);
      }

    }

Viewing all articles
Browse latest Browse all 747

Trending Articles