Commit 272b3d33 authored by Evan Raskob's avatar Evan Raskob

after lesson04 with led slider control and servo serial

parent 501add36
#include <Servo.h>
/*
An example showing how to define a simple call and response serial API
between Arduino and Processing.
*/
Servo myservo;
void setup() {
Serial.begin(57600);
while (!Serial);
myservo.attach(9);
}
unsigned int startIndex = 0; // start index for buffer
......@@ -106,6 +113,11 @@ void loop() {
Serial.println("ERROR MESSAGE HERE");
}
}
// Example: /l,0,0,0 --> turn off all LEDs
// Example: /l,1,0,0 --> turn on 1st LED (13) and off all other LEDs (11,12)
// Example: /l,1,1,1 --> turn on all LEDs (13,11,12)
else if (cmd == 'l') //LEDs?
{
if (a != INVALID && b != INVALID && c != INVALID)
......@@ -114,12 +126,21 @@ void loop() {
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
if (a > 1)
digitalWrite(13,HIGH);
digitalWrite(13, HIGH);
else
digitalWrite(13, LOW);
if (b > 1)
digitalWrite(12, HIGH);
else
digitalWrite(13,LOW);
digitalWrite(12, LOW);
if (c > 1)
digitalWrite(11, HIGH);
else
digitalWrite(11, LOW);
}
// echo back command in same format -- no really needed here, though
// Serial.print('/');
// Serial.print(cmd);
......@@ -131,6 +152,15 @@ void loop() {
// Serial.println(c); // send value, end command
}
// Example: /s,0,179 --> move servo #0 to 180 degrees
else if (cmd == 's') //servo control
{
if (a != INVALID && b != INVALID)
{
myservo.write(b);
}
}
charsRead = 0; // go back to beginning of buffer for next command
startIndex = endIndex; //restart ring buffer with current index
......
......@@ -27,27 +27,39 @@ NetAddress myRemoteLocation;
OscP5 oscP5;
ControlP5 gui;
int sliderValue = 0; // value of the slider, auto updated
int led1Value = 0; // value of the slider, auto updated
int led2Value = 0; // value of the slider, auto updated
int led3Value = 0; // value of the slider, auto updated
void setup()
{
size(800, 600);
frameRate(20);
frameRate(60);
// start oscP5, listening for incoming messages at wekinator port
oscP5 = new OscP5(this, inPort);
myRemoteLocation = new NetAddress(ipAddress, outPort);
gui = new ControlP5(this);
// add a horizontal sliders, the value of this slider will be linked
// add a horizontal sliders, the value of this slider will be linked
// to variable 'sliderValue'
gui.addSlider("sliderValue")
.setPosition(20,20)
.setRange(0,5)
;
gui.addSlider("led1Value")
.setPosition(20, 20)
.setRange(0, 255)
;
gui.addSlider("led2Value")
.setPosition(20, 30)
.setRange(0, 255)
;
gui.addSlider("led3Value")
.setPosition(20, 40)
.setRange(0, 255)
;
}
void draw()
......@@ -60,15 +72,17 @@ void draw()
oscMsg.add(i); // port A0 to A5
oscP5.send(oscMsg, myRemoteLocation);
}
OscMessage mosc = new OscMessage("/l");
int[] vals = {sliderValue,1,2};
mosc.add(vals);
mosc.add(led1Value);
mosc.add(led2Value);
mosc.add(led3Value);
oscP5.send(mosc, myRemoteLocation);
//this next line is because things might happen out of order
synchronized(oscMessagesReceived) {
// handle incoming messages from oscEvent()
for (int i=0; i < oscMessagesReceived.size(); i++)
{
......@@ -77,10 +91,21 @@ void draw()
//print(command + ": ");
String types = msg.typetag();
//println(types);
// draw all analogue inputs as rainbow rectangles
if (command.equals("/a") && types.equals("ii")) {
int analogueIndex = msg.get(0).intValue();
int analogueVal = msg.get(1).intValue();
if (analogueIndex == 0)
{
OscMessage sosc = new OscMessage("/s");
sosc.add(analogueIndex);
sosc.add(int(map(analogueVal,0,1023,0,1)));
oscP5.send(sosc, myRemoteLocation);
}
// draw a corresponding rectangle
float x = width/6.0 * msg.get(0).intValue();
float y = map(msg.get(1).intValue(), 0, 1023, 0, height-10);
......
int inPort = 6161;//6449; // udp input, if needed (not used right now) //<>//
//<>//
int inPort = 6161;//6449; // udp input, if needed (not used right now)
int outPort = 6969; //6448; // wekinator port or unity3d osc port, depending
String ipAddress = "127.0.0.1"; // our computer
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment