Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ICM Final Presentation: This sketch creates projections from pulse input from a Polar heart sensor
#include <RFM69.h>
#include <SPI.h> // the RFM69 library uses SPI
int ledPin = 9;
RFM69 radio;
#define myFrequency RF69_915MHZ // or RF69_433MHZ (check your radio)
int myNetwork = 147; // radios must share the same network (0-255)
int myID = 0; // radios should be given unique ID's (0-254, 255 = BROADCAST)
// our pre-defined packet structure
// this struct must be shared between all nodes
typedef struct {
int sensor0;
int sensor1;
// int sensor2;
// int sensor3;
} Packet;
void setup() {
Serial.begin(9600);
// setup the radio
radio.initialize(myFrequency, myID, myNetwork);
pinMode(ledPin, OUTPUT);
// Serial.println("\nRADIO INITIALIZED\n");
// Serial.println("Listening for sensor nodes...");
}
void loop() {
// always check to see if we've received a message
if (radio.receiveDone()) {
// if the received message is the same size as our pre-defined Packet struct
// then assume that it is actually one of our Packets
// if (radio.DATALEN == sizeof(Packet)) {
// convert the radio's raw byte array to our pre-defined Packet struct
Packet newPacket = *(Packet*)radio.DATA;
int senderID = radio.SENDERID;
// if requested, acknowledge that the packet was received
// if (radio.ACKRequested()) {
// radio.sendACK();
// }
Serial.print("(");
Serial.print(senderID);
Serial.print(")\t");
Serial.print(newPacket.sensor0);
Serial.print("\t");
Serial.println(newPacket.sensor1);
if (newPacket.sensor0 == 1) {
for (int i = 0; i < 20; i++){
digitalWrite(ledPin, HIGH);
Serial.print("(");
Serial.print(senderID);
Serial.print(")\t");
Serial.print("1");
Serial.print("\t");
Serial.println(newPacket.sensor1);
}
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
// Serial.print("\t");
// Serial.print(newPacket.sensor2);
// Serial.print("\t");
// Serial.println(newPacket.sensor3);
// }
// else {
// Serial.println("got unknown packet!");
}
}
//serial variables
var serial; // variable to hold an instance of the serialport library
var portName = '/dev/cu.usbserial-AI03L0YB'; // fill in your serial port name here
//global color variables
var color1 = 0;
var color2 = 255;
// sensor variables
var myID = 0;
var heart = 0;
var breath = 0;
//ball variables
var start = 500;
var target = 1000;
var diameter = start;
var light = 50;
var dark = 200;
var hueValue = light;
var lerpAmt = 0.3;
var state = 'ascending';
//flower variables
var blendModeState = 'on';
//particles variables
var beat = 2;
var r = 0; //radius
var angle = 0;
var posX = 0;
var posY = 0;
function setup() {
createCanvas(1920, 1080);
smooth();
noStroke();
noCursor();
serial = new p5.SerialPort(); // make a new instance of the serialport library
// serial.on('list', printList); // set a callback function for the serialport list event
serial.on('connected', serverConnected); // callback for connecting to the server
serial.on('open', portOpen); // callback for the port opening
serial.on('data', serialEvent); // callback for when new data arrives
serial.on('error', serialError); // callback for errors
serial.on('close', portClose); // callback for the port closing
// serial.list(); // list the serial ports
serial.open(portName);
}
/////////////////////////// Serial Functions ///////////////////////////
// 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:
println(i + " " + portList[i]);
}
}
function serverConnected() {
println('connected to server.'); }
function portOpen() {
println('the serial port opened.');
}
function serialEvent() {
// read a string from the serial port
// until you get carriage return and newline:
var inString = serial.readStringUntil('\r\n');
//check to see that there's actually a string there:
if (inString.length > 0) {
var sensors = split(inString, '\t'); // split the string on the commas
if (sensors.length > 2) { // if there are three elements
myID = sensors[0];
if ( myID === '(1)') {
heart = sensors[1];
breath = sensors[2];
console.log(myID + ", " + heart + ", " + breath);
}
}
}
}
function serialError(err) {
println('Something went wrong with the serial port. ' + err);
}
function portClose() {
println('The serial port closed.');
}
/////////////////////////// End Serial Functions ///////////////////////////
function draw() {
if (key == 1) {
ball();
}
if (key == 2) {
flower();
}
if (key == 3) {
particles();
}
if (key == 4) {
bezierLines();
}
}
function mousePressed() {
if (color1 == 0) {
color1 = 255;
color2 = 0;
blendModeState = 'off';
} else {
color1 = 0;
color2 = 255;
blendModeState = 'on';
}
// if (value == 0) {
// value = 255;
// } else {
// value = 0;
// }
}
function ball() {
background(color2, 20);
if (heart == 1) {
// target = target; //map this depending on input from stretch sensor
state = 'ascending';
}
if (heart == 0 ) {
state = 'descending';
}
if (state == 'ascending') {
diameter = lerp(diameter, target, lerpAmt);
hueValue = lerp(hueValue, dark, lerpAmt);
}
if (state == 'descending') {
diameter = lerp(diameter, start, lerpAmt);
hueValue = lerp(hueValue, light, lerpAmt);
}
fill(70, hueValue);
ellipse(width/2, height/2, diameter, diameter);
}
function flower() {
angleMode(RADIANS);
// frameRate(10);
background(color1,10);
// fill(color2);
if (blendModeState == 'on') {
blendMode(ADD);
}
var variant = heart*500;
var sinValue = abs((sin(frameCount*0.05)))*50 + 50;
translate(width/2, height/2);
rotate(frameCount*0.005);
for (var i = 0; i < 20; i++) {
rotate(PI * 0.6);
fill(color2, random(5));
ellipse(random(100,250), random(0,5), random(sinValue,sinValue+10)*4+variant, sinValue + random(10));
}
for (var i = 0; i < 20; i++) {
rotate(PI * 0.9);
fill(color2,random(5));
ellipse(random(100,250), random(0,5), random(sinValue,sinValue+10)*4+variant, sinValue + random(10));
}
}
function particles() {
angleMode(DEGREES);
background(color1, 70);
// heart = Math.round(random(0, 1));
// posX = lerp(posX, mouseX, 0.1);
// posY = lerp(posY, mouseY, 0.1);
translate(width/2, height/2);
for (var i = 0; i < 2000; i++) {
// rotate(PI * 0.6);
r = random(0, width / 7);
if (heart == 1) {
r = r * beat;
}
angle = PI * 2 * r * (sin(frameCount * 30) + 10);
// if (random(1) < 0.1) {}
// if (frameCount % 10 == 0) {}
stroke(color2, random(200, 255));
strokeWeight(random(1, 4));
point(posX + (r * cos(angle)), posY + (r * sin(angle)));
}
for (var i = 0; i < 3000; i++) {
r = random(0, width / 2);
angle = (0, PI * 2 * r);
stroke(color2, random(200, 255));
strokeWeight(random(0.5, 1));
point(posX + (r * cos(angle)), posY + (r * sin(angle)));
}
for (var i = 0; i < 500; i++) {
r = random(0, width);
angle = (0, PI * 2 * r);
stroke(color2, random(200, 255));
strokeWeight(random(1, 3));
point(posX + (r * cos(angle)), posY + (r * sin(angle)));
}
}
function bezierLines() {
angleMode(RADIANS);
background(color1, 20);
var pointX1 = -2500;
var pointY1 = 0;
var pointX2 = 1000 / 2 - 10;
var pointY2 = 0;
var input = random(1000/8, 1000/2);
var strokeAmount = heart * 3;
translate(width / 2, height / 2);
// stroke(255, 0, 0);
// strokeWeight(20);
// point(pointX1, pointY1);
// point(pointX2, pointY2);
for (var i = 0; i < 50; i++) {
rotate(PI * 0.5);
var controlPointX1 = pointX1;
var controlPointY1 = pointY1;
var controlPointX2 = -input * 2 + i;
var controlPointY2 = -input * 2 + i;
noFill();
stroke(color2);
strokeWeight(strokeAmount);
bezier(
pointX1, pointY1,
controlPointX1, controlPointY1,
controlPointX2, controlPointY2,
pointX2, pointY2
);
}
for (var i = 0; i < 50; i++) {
rotate(PI * 0.7);
var controlPointX1 = pointX1;
var controlPointY1 = pointY1;
var controlPointX2 = -input * 2 + i;
var controlPointY2 = -input * 2 + i;
noFill();
stroke(color2);
strokeWeight(strokeAmount);
bezier(
pointX1, pointY1,
controlPointX1, controlPointY1,
controlPointX2, controlPointY2,
pointX2, pointY2
);
}
}
#include <RFM69.h>
#include <SPI.h> // the RFM69 library uses SPI
RFM69 radio;
#define myFrequency RF69_915MHZ // or RF69_433MHZ (check your radio)
int myNetwork = 147; // radios must share the same network (0-255)
int myID = 1; // radios should be given unique ID's (0-254, 255 = BROADCAST)
int hubID = 0; // the receiver for all sensor nodes in this example
// instead of sending a string, we can send a struct
// this struct must be shared between all nodes
typedef struct {
int sensor0;
int sensor1;
//int sensor2;
//int sensor3;
} Packet;
int ledPin = 5;
///////////////////////////
///////////////////////////
///////////////////////////
void setup() {
// setup the radio
radio.initialize(myFrequency, myID, myNetwork);
// this example only uses Serial inside setup()
// because Narcoleptic will stop Serial once used
Serial.begin(9600);
Serial.println("\nRADIO INITIALIZED");
Serial.println("Sending sensor values");
pinMode(ledPin, OUTPUT);
pinMode(6, INPUT);
}
///////////////////////////
///////////////////////////
///////////////////////////
void loop() {
delay(50);
// create new instance of our Packet struct
Packet packet;
packet.sensor0 = digitalRead(6); // read values from the analog pins
packet.sensor1 = analogRead(A0);
int numberOfRetries = 5;
// send reliable packet to the hub
// notice the & next to packet when sending a struct
boolean gotACK = radio.sendWithRetry(hubID, &packet, sizeof(packet), numberOfRetries);
if (gotACK) {
Serial.println("got acknowledgment");
}
else {
Serial.println("failed acknowledgment");
}
int heartState = digitalRead(6);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (heartState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment