Skip to content

Instantly share code, notes, and snippets.

@halfbyte
Created November 29, 2013 22:43
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save halfbyte/7712995 to your computer and use it in GitHub Desktop.
Arduino experiment to drive some DIODER from IKEA via OSC by using an Arduino and a small ruby script.
#include <SPI.h>
#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <util.h>
// color swirl! connect an RGB LED to the PWM pins as indicated
// in the #defines
// public domain, enjoy!
#define REDPIN 5
#define GREENPIN 6
#define BLUEPIN 3
#define FADESPEED 5 // make this higher to slow down
EthernetUDP Udp;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
void setup() {
byte mac[] = {1,2,3,4,5,6};
int success = 0;
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
Serial.begin(9600);
success = Ethernet.begin(mac);
if (success) {
Serial.println("My IP address is:");
Serial.println(Ethernet.localIP());
}
Udp.begin(8888);
}
void loop() {
int packetSize = Udp.parsePacket();
if(packetSize)
{
IPAddress remote = Udp.remoteIP();
// read the packet into packetBufffer
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
if (packetBuffer[0] == 'S') {
analogWrite(REDPIN, (int)packetBuffer[1]);
analogWrite(GREENPIN, (int)packetBuffer[2]);
analogWrite(BLUEPIN, (int)packetBuffer[3]);
}
if (packetBuffer[0] == 'R') {
analogWrite(REDPIN, (int)packetBuffer[1]);
}
if (packetBuffer[0] == 'G') {
analogWrite(GREENPIN, (int)packetBuffer[1]);
}
if (packetBuffer[0] == 'B') {
analogWrite(BLUEPIN, (int)packetBuffer[1]);
}
}
}
#!/usr/bin/env ruby
require 'rubygems'
require 'socket'
require 'osc'
ip = ARGV[0]
HOST = '0.0.0.0'
PORT = 5000
u2 = UDPSocket.new
u2.connect(ip, 8888)
s = OSC::UDPServer.new
s.bind HOST, PORT
s.add_method '/led/*', 'f' do |msg|
domain, port, host, ip = msg.source
color = msg.address.split("/").last
u2.send(color.upcase + (255 * msg.args[0]).to_i.chr, 0);
end
s.serve
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment