Created
October 21, 2019 17:09
-
-
Save smanning29/5c8979a140d3d08aa952bc95ee8e973e to your computer and use it in GitHub Desktop.
Serial
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
void setup() { | |
Serial.begin(9600); | |
pinMode(7, OUTPUT); | |
} | |
void loop() { | |
int p1 = analogRead(A0); | |
int p2 = analogRead(A1); | |
int p3 = analogRead(A2); | |
int p1val = map(p1, 0, 1023, 0, 255); | |
int p2val = map(p2, 0, 1023, 0, 255); | |
int p3val = map(p3, 0, 1023, 0, 255); | |
Serial.print(p1val); | |
Serial.print(","); | |
Serial.print(p2val); | |
Serial.print(","); | |
Serial.println(p3val); | |
//Serial.write(myVal); | |
char p5 = Serial.read(); | |
if(p5 == 'c'){ | |
digitalWrite(7, HIGH); | |
} | |
else{ | |
digitalWrite(7, LOW); | |
} | |
} |
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
/* Template from Arielle Hein */ | |
/* Color matching game */ | |
var serial; //variable to hold an instance of the serial port library | |
var portName = '/dev/tty.usbmodem14101'; | |
var inData; | |
var R; var G; var B; | |
var p1; var p2; var p3; | |
var rVal; var gVal; var bVal; | |
//PFont font; | |
function setup() { | |
createCanvas(600, 600); | |
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); | |
//set colors | |
R = Math.round(random(0, 255)); | |
G = Math.round(random(0, 255)); | |
B = Math.round(random(0, 255)); | |
console.log("R: "+R+" G: "+G+" B: " +B); | |
} | |
function draw() { | |
background(255, 255, 255); | |
rect(0, 350, 600, 250); | |
fill(R, G, B); | |
noStroke(); | |
rect(0, 0, 600, 250); | |
fill(p1,p2,p3); | |
var rTrue = false; | |
var gTrue = false; | |
var bTrue = false; | |
if(p1 > R-5 && p1 < R+5){ | |
textSize(32); | |
text('R', 110, 290, 300, 350); | |
//fill(255, 0, 0); | |
textAlign(CENTER); | |
rTrue = true; | |
} | |
if(p2 > G-5 && p2 < G+5){ | |
textSize(32); | |
text('G', 140, 290, 300, 350); | |
//fill(0, 255, 0); | |
textAlign(CENTER); | |
gTrue = true; | |
} | |
if(p3 > B-5 && p3 < B+5){ | |
textSize(32); | |
text('B', 170, 290, 300, 350); | |
//fill(255, 0, 0); | |
textAlign(CENTER); | |
bTrue = true; | |
} | |
if(rTrue == true && gTrue == true && bTrue == true){ | |
serial.write('c'); | |
console.log("SENT"); | |
} | |
else{ | |
serial.write('h'); | |
} | |
} | |
function serverConnected() { | |
console.log('connected to the server'); | |
} | |
function portOpen() { | |
console.log('the serial port opened!'); | |
} | |
//THIS IS WHERE WE RECEIVE DATA!!!!!! | |
//make sure you're reading data based on how you're sending from arduino | |
function serialEvent() { | |
inData = serial.readLine(); | |
if(inData.length != 0){ | |
sensorReadings = split(inData, ","); | |
p1 = Number(sensorReadings[0]); | |
p2 = Number(sensorReadings[1]); | |
p3 = Number(sensorReadings[2]); | |
} | |
} | |
function serialError(err) { | |
console.log('something went wrong with the port. ' + err); | |
} | |
function portClose() { | |
console.log('the port was closed'); | |
} | |
// get the list of ports: | |
function printList(portList) { | |
// portList is an array of serial port names | |
for (var i = 0; i < portList.length; i++) { | |
// Display the list the console: | |
// print(i + " " + portList[i]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment