xxxxxxxxxx
97
let osc;
let binArray=[0,1,0,1,1,0,1];
let noteCount;
let binIndex=0;
let restTimer=0;
let val;
let delineator;
let delinCounter=0;
let nextEventTime;
amp=1
function setup() {
createCanvas(400, 400);
let cnv = createCanvas(100, 100);
cnv.mousePressed(playOscillator);
osc = new p5.Oscillator('sine');
delineator=false;
noteCount=0;
nextEventTime=0;
}
function draw() {
frameRate(60);
background(200);
freq=300;
val=binArray[binIndex];
if(val==0)
{
freq=500;
} else {
freq=50;
}
//delineator=!delineator;
noteCount++;
//print(noteCount);
if(noteCount>20)
{
noteCount=0;
if(!delineator)
{
binIndex++;
delineator=true;
}
if(delineator)
{
delineator=false;
}
}
if(binIndex > binArray.length-1)
{
restTimer++;
amp=0;
if(restTimer > 100)
{
amp=1
binIndex=0;
restTimer=0;
}
}
osc.freq(freq,0);
osc.amp(amp,0);
print(binIndex);
}
function playOscillator() {
osc.start();
playing = true;
}
//on arduino side
//3 possible messages
//freq A, freq B, or rest (between vals)
//first, wait for 1000 zeroes to be counted(the rest step) then open up the listening loop
//simply count the number of 1s coming in
//and keep track of how many zeroes in a row
//if more than 10 zeroes in a row,
//check how many 1s were counted so far
//if it's a high number (from high freq), then write a bit of 1 to the current index and index++, if it's a lower number, write a bit of 0 to the current index, and index++