Created
May 22, 2019 23:05
-
-
Save scealux/2afbc58106d8f5bbfa9c89ff9c9a40b0 to your computer and use it in GitHub Desktop.
An Arduino with a potentiometer communicates back and forth with a HSB selector running in p5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adafruit NeoPixel - Version: Latest | |
#include <Adafruit_NeoPixel.h> | |
#ifdef __AVR__ | |
#include <avr/power.h> | |
#endif | |
//set out signal pin | |
#define PIN 9 | |
Adafruit_NeoPixel strip = Adafruit_NeoPixel(5, PIN, NEO_GRB + NEO_KHZ800); | |
int potPin = 0; | |
bool grammar = 0; | |
int serialH = 0; | |
int serialS = 0; | |
int serialB = 0; | |
bool update = true; | |
void setup() { | |
//start the strip | |
strip.begin(); | |
//initialize all pixels to 'off' | |
strip.show(); | |
Serial.begin(9600); | |
} | |
void loop() { | |
//SERIAL IN ------------------------------------ | |
if (Serial.available() > 0){ | |
int inByte = Serial.read(); //Read in value | |
if(inByte > 0 && inByte <= 200){ // (0-200] HUE | |
serialH = inByte; | |
update = true; | |
}else if(inByte > 200 && inByte <= 230){ // (200 - 230] SATURATION | |
serialS = inByte - 200; | |
update = true; | |
}else if(inByte > 230 && inByte <=255){ // (230 - 255] BRIGHTNESS | |
serialB = inByte - 230; | |
update = true; | |
}else{ | |
update = false; | |
} | |
} | |
//---------------------------------------------- | |
int ledNum = map(analogRead(potPin),0,1010,0,5); //Read map the potentiometer to number of LED's shown | |
if(ledNum == 1){ // Decide if we need an 's for the LED label | |
grammar = 0; | |
}else{ | |
grammar = 1; | |
} | |
int pixelH = map(serialH, 0, 200, 0, 65536); //map the incoming values from p5 to the values the Neopixel library needs | |
int pixelS = map(serialS, 0, 29, 0, 255); | |
int pixelB = map(serialB, 0, 24, 0, 100); | |
if (update){ // If the values have changed, update the strip | |
for(uint16_t i=0; i<strip.numPixels(); i++){ //first wipe the strip | |
strip.setPixelColor(i,0,0,0); | |
} | |
for(uint16_t i=0; i<ledNum; i++){ | |
strip.setPixelColor(i,strip.ColorHSV(pixelH, pixelS, pixelB)); //Then change the LED colors for the correct number of LED's | |
strip.show(); | |
} | |
}; | |
//SERIAL OUT ----------------------------------- | |
Serial.println(String(ledNum) + " " + String(grammar) + " " + String(pixelH) + " " + String(pixelS) + " " + String(pixelB)); //Serial out the values to p5, last 3 are for debug. | |
//---------------------------------------------- | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var serial; //variable to hold an instance of the serial port library | |
var portName = 'COM3'; //fill in with YOUR port | |
var data = [0,1]; | |
var numLED = 0; | |
function setup() { | |
createCanvas(400, 300); | |
textSize(15); | |
hSlider = createSlider(0, 100, 0); | |
hSlider.position(20, 20); | |
hSlider.style('width', '320px'); | |
sSlider = createSlider(0, 255, 240); | |
sSlider.position(20, 50); | |
sSlider.style('width', '320px'); | |
bSlider = createSlider(0, 100, 50); | |
bSlider.position(20, 80); | |
bSlider.style('width', '320px'); | |
serial = new p5.SerialPort(); //a new instance of serial port library | |
//set up events for serial communication | |
serial.on('connected', serverConnected); | |
serial.on('open', portOpen); | |
serial.on('data', serialEvent); | |
serial.on('error', serialError); | |
serial.on('close', portClose); | |
//open our serial port | |
serial.open(portName); | |
} | |
function draw() { | |
//numLED = data[1]; | |
background('white'); | |
textSize(14); | |
text('H', hSlider.x * 2 + hSlider.width, 35); | |
text('S', sSlider.x * 2 + sSlider.width, 65); | |
text('B', bSlider.x * 2 + bSlider.width, 95); | |
colorMode(HSB,100); | |
fill(hSlider.value(),sSlider.value(),bSlider.value()); | |
noStroke(); | |
rect(20,120,360,160); | |
if (bSlider.value() < 75 || sSlider.value() < 125){ | |
fill(0,0,255); | |
}else { | |
fill(0,0,0); | |
} | |
textAlign(CENTER); | |
textSize(40); | |
var message; | |
if (data[1] == 1){ | |
message = " LEDs" | |
}else{ | |
message = " LED" | |
}; | |
text(String(data[0]) + message, width/2, 215); | |
fill(0,0,0); | |
//console.log(String(data[0])+ " " + String(data[1])); | |
//serial.write(hSlider.value() + " " + sSlider.value() + " " + bSlider.value()); | |
hSerial = Math.floor(map(hSlider.value(), 0, 100, 0, 200)); | |
sSerial = Math.floor(201+ map(sSlider.value(), 0, 255, 0, 29)); | |
bSerial = Math.floor(231+ map(bSlider.value(), 0, 100, 0, 24)); | |
console.log("p5 Hue: " + hSerial + " Saturation: " + sSerial + " Brightness: " + bSerial); | |
serial.write(hSerial); | |
serial.write(sSerial); | |
serial.write(bSerial); | |
console.log("Arduino Hue: " + data[2] + " Saturation: " + data[3] + " Brightness: " + data[4]); | |
} | |
function serverConnected(){ console.log('connected to the server');} | |
function portOpen(){ console.log('the serial port opened!');} | |
function serialEvent(){ | |
var inString = serial.readLine(); | |
if (inString.length > 0){ | |
data = split(inString, " "); | |
} | |
} | |
function serialError(err){ console.log('something went wrong with the port. ' + err);} | |
function portClose(){ console.log('the port was closed')} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment