Skip to content

Instantly share code, notes, and snippets.

@SophieMcD
Last active December 28, 2015 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SophieMcD/7489871 to your computer and use it in GitHub Desktop.
Save SophieMcD/7489871 to your computer and use it in GitHub Desktop.
Processing & Arduino code for the Sailing By boat project
// Project by Sophie McDonald
//much of this code has come from Arduino examples, including:
//ReadACSIIString by Tom Igoe
//stepper_oneRevolution by Tom Igoe
// with help from Vincent Ackermans
// infomation from http://forum.arduino.cc/index.php?topic=142058.0
#include <Stepper.h>
// Incoming Data Variables
int swellPeriodIN;
int windSpeedIN;
// other Variables
int waveSpeed;
int fanSpeed;
// Pin Vairiables
int fanPin = 3;
// Stepper Library
const int stepsPerRevolution = 1024; // divisable by 8, the stepper phase.
Stepper waveStepper(stepsPerRevolution,4,5,6,7);// Stepper motor pins.
void setup(){
waveStepper.setSpeed(10);
pinMode(fanPin,OUTPUT);
Serial.begin(9600);
}
void loop(){
// incoming data - weather direction in degrees
// map to º - I'm guessing 360 would actually be 0.
// The step angle is 5.625° / 64
while (Serial.available() > 0) { // do this only when data is recieved
///READ INCOMING DATA
swellPeriodIN = Serial.parseInt();
windSpeedIN = Serial.parseInt();
// Serial.println("swellPeriodIN,windSpeedIN"); //test
if (Serial.read() == '\n') { // when last part of incoming string recieved, do this...
analogWrite(fanPin,windSpeedIN);
/// wave stepper
waveStepper.setSpeed(60000/swellPeriodIN); //swellPeriod in miliseconds gives the speed in seconds to the .setstepper()
delay(10); //
waveStepper.step(stepsPerRevolution);
}
}
}
// Project by Sophie McDonald
// with help from Vincent Ackermans
//The communication elements of this code were taken from Arduino example PhysicalPixel by Tom Igoe
//talking to the Arduino
import processing.serial.*;
Serial port;
boolean found = false;
JSONObject json;
void setup(){
//taken from Arduino example PhysicalPixel
//talking to the Arduino
println(Serial.list());
port = new Serial(this, Serial.list()[0], 9600);
}
void draw(){
// put feed in here
read();
}
void read() {
json = loadJSONObject("WWO.json"); //static file
// Nested data for World Weather Online:
JSONObject data = json.getJSONObject("data");
JSONObject weather = data.getJSONArray("weather").getJSONObject(0);
//println("weather"+weather.getString("date"));
JSONArray hourly = weather.getJSONArray("hourly");
JSONObject seaData = hourly.getJSONObject(0);
String swellPeriod = seaData.getString("swellPeriod_secs");
String windspeedMiles = seaData.getString("windspeedMiles");
//////// SCALE VALUES to Stepper values 256 steps per revolution. http://forum.arduino.cc/index.php?topic=142058.0 /////////
float windspeedMilesMap = map(float(windspeedMiles),0,80,0,255); // map to PWM resolution max 255
float swellToMilSeconds = float(swellPeriod) *1000; // divide the number of swell seconds by 1024 for the stepper speed
windspeedMilesMap = constrain(windspeedMilesMap,0,1024);
println("swellPeriod "+swellPeriod);
println("windspeedMiles "+windspeedMiles);
String dataString = new String(int(swellToMilSeconds) + " " + int(windspeedMilesMap) + "\n");
print(dataString);
port.write(dataString);
delay(int(swellPeriod)*1000); // delay loop to limit calls to the website (there is a 3 times per sec limit on a free account.)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment