Friday, May 21, 2010

Button to Servo

First attempt at connecting two Arduinos over Pachube - one wired up with a simple digital input sending a 0 (off) or a 1 (on) to a Pachube feed. The Arduino on the second computer retrieves and reads the feed. If the recieved input is 0, the servo is set to 0*. If the input received is 1 (button is pushed), the servo turns to 180*.

This was successfully achieved with no major problems other than simply syntax errors. The codes used are as follows:

Send button push (using Processing):

// Code was adapted from eeml_manual_button example and is run with StandardFirmata uploaded onto the Arduino board.
// This code sends a value of 0 or 1 from a digital input attached to pin 2.
// Uploads to a Pachube feed every 5 seconds.

import eeml.*;
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;


DataOut dOut;

float lastUpdate;
int buttonState =0;

void setup(){
// set up DataOut object; requires URL of the EEML you are updating, and your Pachube API key
dOut = new DataOut(this, "FEED LINK HERE",
"API CODE HERE");

// and add and tag a datastream
dOut.addData(2,"button");
arduino = new Arduino (this, Arduino.list()[0],57600);
arduino.pinMode(2,arduino.INPUT);

}


void draw()
{

// update once every 5 seconds (could also be e.g. every mouseClick)
if ((millis() - lastUpdate) > 5000){
buttonState=arduino.digitalRead(2);

println("ready to POST: ");
dOut.update(0, buttonState); // update the datastream
int response = dOut.updatePachube(); // updatePachube() updates by an authenticated PUT HTTP request
println(response); // should be 200 if successful; 401 if unauthorized; 404 if feed doesn't exist
lastUpdate = millis();
}
}


Receive button push and control servo (using Processing):

// This code was adapted from eeml_auto_request_arduino and is run with ServoFirmata uploaded ont
o the Arduino board.
// - A basic example to retrieve data from an existing Pachube feed
// This code reads the value from the data feed every 5 seconds and controls the servo position accordingly.

import eeml.*;
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;

float myVariable;

//int servoPin = 10; // Control pin for servo motor

DataIn dIn;

void setup(){

size (180, 50);

// set up DataIn object; indicate the URL you want, your Pachube API key, and how often you want it to update
// e.g. every 15 seconds
dIn = new DataIn(this,"FEED LINK HERE",
"API CODE HERE", 5000);

arduino = new Arduino(this, Arduino.list()[0]);

arduino.pinMode(10, Arduino.OUTPUT);
}

void draw()
{
if (myVariable == 0.0) { // Button is off.
arduino.analogWrite(10, 0); // Set servo position to 0 degrees.

println("off");
} else { //Button is on.
println("on");
arduino.analogWrite(10, 180); // Set servo position to 180 degrees.
}
}

// onReceiveEEML is run every time your app receives back EEML that it has requested from a Pachube feed.

void onReceiveEEML(DataIn d){
myVariable = d.getValue(2); // get the value of the stream 1
println(myVariable);


}

Next step from here is to wire up both circuits with both a button and a servo and recode so that both can send and receive data from Pachube. But to finish off, here are the faces behind it.



No comments:

Post a Comment