Skip to content

Instantly share code, notes, and snippets.

@lisajamhoury
Created November 17, 2015 03:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lisajamhoury/d7d3a56aa81eecde29f0 to your computer and use it in GitHub Desktop.
Save lisajamhoury/d7d3a56aa81eecde29f0 to your computer and use it in GitHub Desktop.
Heart Rate to P5 Bezier Prototype 1
//include filter libraries
#include <FilterDerivative.h>
#include <FilterOnePole.h>
#include <Filters.h>
#include <FilterTwoPole.h>
#include <FloatDefine.h>
#include <RunningStatistics.h>
#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 = 123; // 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;
//filtering
float bandpass_lo = 50;
float bandpass_hi = 200;
// create a one pole (RC) lowpass filter
FilterOnePole lowpassFilter( LOWPASS, bandpass_hi);
FilterOnePole highpassFilter( HIGHPASS, bandpass_lo);
///////////////////////////
///////////////////////////
///////////////////////////
void setup() {
Serial.begin(9600);
// setup the radio
radio.initialize(myFrequency, myID, myNetwork);
// 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");
// int filtered1 = lowpassFilter.input( analogRead( newPacket.sensor0 ) );
//int filtered2 = highpassFilter.input( analogRead( filtered1 ) );
// Serial.println(filtered2);
Serial.println(newPacket.sensor0);
// Serial.print("\t");
// Serial.print(newPacket.sensor1);
// Serial.print("\t");
// Serial.print(newPacket.sensor2);
// Serial.print("\t");
// Serial.println(newPacket.sensor3);
}
else {
Serial.println("got unknown packet!");
}
}
}
var serial; // variable to hold an instance of the serialport library
var portName = '/dev/cu.usbserial-DA01ILEL'; // fill in your serial port name here
var input;
function setup() {
createCanvas(1280, 800);
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) {
if (inString !== 'hello'){
var inputRaw = inString; // input becomes content of string
input = map(inputRaw, 0, 1023, 0, width/2); // map raw input to half width
println(input);
}
serial.write('x'); //send a byte to request data
}
}
function serialError(err) {
println('Something went wrong with the serial port. ' + err);
}
function portClose() {
println('The serial port closed.');
}
/////////////////////////// End Serial Functions ///////////////////////////
function draw() {
background(0);
var pointX1 = -100;
var pointY1 = 0;
var pointX2 = width / 2 - 10;
var pointY2 = 0;
// var input = random(0, width/2);
translate(width / 2, height / 2);
// stroke(255, 0, 0);
// strokeWeight(20);
// point(pointX1, pointY1);
// point(pointX2, pointY2);
for (var i = 0; i < 300; i++) {
rotate(PI * 0.5);
var controlPointX1 = pointX1;
var controlPointY1 = pointY1;
var controlPointX2 = -input * 2 + i;
var controlPointY2 = -input * 2 + i;
noFill();
stroke(255, 255, 255, 50);
strokeWeight(1);
bezier(
pointX1, pointY1,
controlPointX1, controlPointY1,
controlPointX2, controlPointY2,
pointX2, pointY2
);
}
for (var i = 0; i < 300; i++) {
rotate(PI * 0.7);
var controlPointX1 = pointX1;
var controlPointY1 = pointY1;
var controlPointX2 = -input * 2 + i;
var controlPointY2 = -input * 2 + i;
noFill();
stroke(255, 255, 255, 50);
strokeWeight(1);
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 = 123; // 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;
///////////////////////////
///////////////////////////
///////////////////////////
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");
}
///////////////////////////
///////////////////////////
///////////////////////////
void loop() {
delay(50);
// create new instance of our Packet struct
Packet packet;
packet.sensor0 = analogRead(A0); // read values from the analog pins
packet.sensor1 = analogRead(A1);
packet.sensor2 = digitalRead(10);
packet.sensor3 = digitalRead(11);
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");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment