Newbie Serial Read question

Hello all, this is my first post to this forum (I posted this to the Processing forum also but this one appears to be more active) so I will apologize up front for my super-newbie question. I just bought my first Arduino board and I am trying to get it to listen to an LED that I have flashing on a seperate breadboard. The breadboard is hooked up to a Basic Stamp microcontroller. It is a simple circuit that switches a pin on the Basic Stamp from HIGH to LOW every 1.5 seconds. I took a jumper and wired it from the same row as the positive end of the LED that is flashing and inserted it into pin 7 on the digital side of the Arduino board. I am using the programs for the Arduino/Porcessesing below to try and detect when the LED is on and off but am sure I am doing something very wrong. Any help would be greatly appreciated. Thanks in advance for any help you can offer.

Arduino code: int ledPin = 13; // Nothing connected to this pin int inPin = 7; // jumper connected to digital pin 7 from BasicStamp int val = 0; // variable to store the read value

void setup() { pinMode(ledPin, OUTPUT); pinMode(inPin, INPUT); }

void loop() { val = digitalRead(inPin); if (val > 0) digitalWrite(ledPin, HIGH); }

Processesing code: import processing.serial.; import cc.arduino.;

Arduino arduino; int ledPin = 13; int val; void setup() { //size(300,300); println(Arduino.list()); arduino = new Arduino(this, Arduino.list()[3], 57600); }

void draw() { // background(off); // stroke(on); if (arduino.digitalRead(ledPin) == Arduino.HIGH){ fill(153); println("grey"); } else { noFill(); println("no fill"); }


If the two boards do not share powersupply, then you have to connect their grounds together.


Also in your Arduino code in the loop you are setting the LED pin high, but never setting it low, so after the first time it goes high, it will remain high.

And you are never sending anything from Arduino to Procesing.

And you are setting the same pin high in the loop as the loop as the basic stam is setting high for you.

You need to detect if the pin is high, and then. send something to processing

First, thank you very much for the reply. So many things to correct I would like to make sure I understand them all.

  1. Grnd - check that seems easy enough.
  2. Resetting the pin to low. Would this work?: val = digitalRead(inPin); if (val > 0) digitalWrite(ledPin, HIGH); delay(500); digitalWrite(ledPin, LOW);
  3. Sending from Arduino to processing: this confuses me because I thought Processesing would do this by "reading" the pin. Would you mind posting a small snippet to show how this is done?
  4. Setting the same pin to high as Basic Stamp: So should I just eliminate that part of the code? Basic Stamp also sets it to LOW in a loop so could I eliminate the code in #2 above altogether and just create whatever I need in #3 to send to processing along with the GRND wire?
  5. Detecting and sending to Processesing: This is my goal for the project, I am grateful for any more help you could offer to get me there. I am working in a new language and have no electronics experience. However, this stuff seems very interesting and I really want to learn about it.

The processing sketch that you are running assumes that you are running firmata on the Arduino, which you are not.

The led pin, in the Arduino sketch is declared to be an output pin. In the Processing sketch, you are then trying to read from it. Were it possible for this sketch to operate, reading from an output pin would fail.

The snippet of code you posted in reply #2 is correct for setting the pin back LOW, as MikMo suggested.

The Arduino can write to the serial port, using Serial.write, Serial. print, and Serial.println (println includes a carriage return/line feed combination; print does not). Serial.print sends data in ascii format. Serial.write sends data in binary format.

Processing has commands to read serial data. Look at some of the examples in Processing for how to do it. Most include code to be loaded on the Arduion, too.


Thank you for the additional information. I can appreciate how newbie questions can be annoying so thanks for taking the time. would you mind providing additional information on this point:

You said that the processesing sketch assumes I am running firmata and that you could tell I am not. Would you mind explaining how you knew that and also if I need to use it for this to work?

Gratefully, Jfish

Would you mind explaining how you knew that

because you said in your original post:-

Arduino code: int ledPin = 13; // Nothing connected to this pin int inPin = 7; // jumper connected to digital pin 7 from BasicStamp

So that is arduino code not the arduino firmata code

Would you mind explaining how you knew that and also if I need to use it for this to work?

Grumpy_Mike took care of the how. Do you need to use firmata? No.

I'd concentrate on the Arduino code first. Turn the LED on the Arduino on when it senses that the stamp LED is on, and turn the LED on the Arduino off when it senses that the stamp LED is off.

When that works, use Serial.println to write "Off" and "On" to the serial port at the appropriate times.

Then, have the Processing application read the serial port data, and color the block accordingly.

You could, once you know that the Arduino is capable of determining that the stamp LED is on or off, upload firmata to the Arduino, and then use the Processing sketch you have to ask the Arduino if the LED is on (if pin 7 is HIGH).

Either approach will work.

Thank you for all the information. I will try it this weekend.

Gratefully, JFish