As computing becomes more ubiquitous in our objects, designers need to be more aware of how to design meaningful interactions into electronically enhanced objects. At the University of Washington, a class of junior Interaction Design majors is exploring this question. These pages chronicle their efforts.

Thursday, June 4, 2015

Getting Processing and Arduino to Communicate [Jessica and Gloria]

Processing can do a lot of things Arduino can't. The Arduino UNO can't pull data off the internet without serious modifications/extra parts so we decided to use Processing to help the Arduino access data on a server.

Read more about Processing in regards to the Arduino HERE

An important part of getting our lamp to work (lamp light based on weather) was finding out how to get Processing to send data to our Arduino Uno.


Weather Server (Yahoo Weather) --> Processing (gets the weather data and feeds it to Arduino in a way that Arduino can understand)--> Arduino (controls the neopixel lights based on the weather information that it gets from Processing)--> Neopixel Lights


Going through this tutorial was EXTREMELY helpful in figuring out how to get Processing to "talk" to the Arduino.

https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all


In the tutorial, we setup a code so that when you clicked on a window on the computer screen, a red LED light located on the Arduino would light up.

Here was the code we used.

IN PROCESSING: 

import processing.serial.*;

Serial myPort;  // Create object from Serial class

void setup()
{
  size(200,200); //make our canvas 200 x 200 pixels big
  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
}

void draw() {
  if (mousePressed == true) 
  {                           //if we clicked in the window
   myPort.write('1');         //send a 1
   println("1");   
  } else 
  {                           //otherwise
  myPort.write('0');          //send a 0
  }   
}

IN ARDUINO

 char val; // Data received from the serial port
 int ledPin = 13; // Set the pin to digital I/O 13

 void setup() {
   pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
   Serial.begin(9600); // Start serial communication at 9600 bps
 }

 void loop() {
   if (Serial.available()) 
   { // If data is available to read,
     val = Serial.read(); // read it and store it in val
   }
   if (val == '1') 
   { // If 1 was received
     digitalWrite(ledPin, HIGH); // turn the LED on
   } else {
     digitalWrite(ledPin, LOW); // otherwise turn it off
   }
   delay(10); // Wait 10 milliseconds for next reading
}


No comments:

Post a Comment