Thursday, May 27, 2010

Back and Forth

We begun to set up an advancement on our previous setup where both Arduinos could send data to separate Pachube feeds via a digital input (a button) while reading the other ones input and using it to determine the position of a servo.


Where this seemed a simple enough advancement, this is where we began to encounter major problems. The coding itself seemed fairly straightforward to simply amalgamate the send and the receive ones used last time:

// Code was adapted from eeml_manual_button example combined with eeml_auto_request_arduino
// This code sends a value of 0 or 1 from a digital input attached to pin 2 and uploads to a Pachube feed every 5 seconds.
// Simultaneously it also reads a seprate feed from another Arduino sending out a digital input and uses this to determine the position of a servo.

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

float myVariable;

DataOut dOut;
float lastUpdate;
int buttonState =0;



//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,"LINK TO FEED BEING READ",
"API", 5000);

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

//*****************************************

dOut = new DataOut(this, "LINK TO FEED BEING POSTED TO",
"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()
{
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.

}

// 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();

}


}

// 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);

}



We had two issues arise here. First of all, in order to control the Arduino board using Processing, Firmata had to be uploaded. In order to send and receive data, StandardFirmata was the most suitable to bridge the two. However, this was not ideal for controlling the servo and when we tried to run the above sketch with StandardFirmata uploaded to the Arduino board, the servo buzzed and wouldn't respond correctly to the input.

Previously, when the servo was just receiving data to control the servo (and not sending), we simply had ServoFirmata uploaded. This was no longer an option as it is crucial we are able to both send and receive data.

The other major roadblock was that while Processing was attempting to read an incoming feed, it was no longer able to update it's own feed.

To deal with one issue at a time, we began to search for a solution by attempting to modify the StandardFirmata sketch to make it successfully control the servo. We quickly found that this was beyond our level of understanding of Arduino and programming in general. We posted for help on the Processing and Arduino forums:

We are a working on creating a program where a button input sends data over the internet via Pachube which is received by another Arduino to control a servo.

We have gotten this successfully working using Processing and Servo Firmata in Arduino (i.e. my button push on my Arduino controls my teammates servo attached to his Arduino). Unfortunately, as we need to both send and recieve data from the same Arduino (send button push data and recieve data to control servo), we need to use the Standard Firmata. This however doesn't successfully control the servo causing it to buzz and not respond to the recieved data.

We have experimented trying to adapt the Firmata codes and using just a simpler code for trouble shooting problems as below. When the following code is run on Processing with Servo Firmata it works beautifully but when run with Standard Firmata, it has the same issue and buzzes / doesn't respond properly.


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

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

void setup(){

size (180, 50);
arduino = new Arduino(this, Arduino.list()[0]);
arduino.pinMode(servoPin, Arduino.OUTPUT);
// note - we are setting a digital pin to output
}

void draw(){
arduino.analogWrite(servoPin, mouseX); // the servo moves to the horizontal location of the mouse
}



If anyone has done anything similar or can think of a solution, any help would be much appreciated from this sleep deprived, caffeine driven group of uni students.


While waiting for a response and running out of time, we began to search for other alternatives in the meantime.

No comments:

Post a Comment