currently happily under construction :)

How to send interpolated analog values from Arduino to p5.js via fake keyboard

Arduino fake keyboard code


#include "Keyboard.h"

// key map tables
int keycodes_axis[] = {48,49,50,51,52,53,54,55,56,57}; // 0123456789
int keycodes_modeswitch[] = {65,83,68,70,71}; // asdfg

int p_axis_keyint = -1;
int p_mode_keyint = -1;

void setup() {
  // initialize a fake keyboard:
  Keyboard.begin();
 }

void loop() {

  // read out all analog values from the Arduino pins
  int poti = analogRead(0); // read analog pin 0 values of 0 - 1024
  float modeselect = analogRead(1); // read analog pin 0 values of 0 - 1024

  // map the input values to the key tables!   keyMapper( input value, keytable, size of keytable )
  int axis_keyint = keyMapper(poti,keycodes_axis,10);
   int mode_keyint = keyMapper(modeselect,keycodes_modeswitch,5);
 
  //avoid sending doubles if loop
  if(axis_keyint != p_axis_keyint){

    Keyboard.write(  axis_keyint );
    p_axis_keyint = axis_keyint; 
    delay(10);
  }

    //avoid sending doubles if loop
  if(p_mode_keyint != mode_keyint){

    Keyboard.write( mode_keyint );
    p_mode_keyint = mode_keyint;
    delay(10);
  }
}

// key mapper function
int keyMapper(int _inval, int _karr[], float _soa ){

    float normalized_in = _inval*.00097; // normalized input to 0 - 1
    normalized_in = constrain(normalized_in,0,1); // limit input in case of crappy input
    int sizeof_array = sizeof(_karr);
   int arr_id = int( normalized_in * _soa );
   
    return _karr[arr_id]; // select from keycode array
 }

p5.js key mapper code

let keycodes_axis = [48,49,50,51,52,53,54,55,56,57]; // 0123456789
let keycodes_modeswitch = [65,83,68,70,71]; // asdfg

let c_axis_val = 0;
let s_axis_val = 0;

let c_mode = 0;

function setup() {
  createCanvas(window.innerWidth, window.innerHeight);
}

function draw() {
  
  
   // just draw a rect for visual feedback
  noStroke();
  fill(0);
  rect( 0,0,width,height * s_axis_val );
  
  
  if(c_mode == 0 ){
    //----------------- MODE 1
    background(00,22);
                  
  }
  
   else if(c_mode == 1 ){
     
     //----------------- MODE 2
    background(40,22);
  
   } 
  
  
   else if(c_mode == 2 ){
     
     //----------------- MODE 3
    background(80,22);
  
   } 
  
   else if(c_mode == 3 ){
     
     //----------------- MODE 4
    background(120,22);
  
   } 
  
  
   else if(c_mode == 4 ){
     
     //----------------- MODE 5
    background(220,22);
  
   } 
  
  //create a smooth value from stepped input values from keyboard
  s_axis_val = lerp(s_axis_val, c_axis_val, .1);
  
}



function keyPressed(){
  
  
  let rav = returnAxisValueFromKeyCode(keyCode, keycodes_axis );
  // only update axis value when new value is found
  if(rav > -1){  c_axis_val = rav *.1;}
  
  let rmv =  returnAxisValueFromKeyCode(keyCode, keycodes_modeswitch );
   // only update mode value when new value is found
  if(rmv > -1){
     c_mode =rmv;
  }


}


// helper function for remapping

function returnAxisValueFromKeyCode( _kC , _arr ){
  
  for(let i=0; i <_arr.length; i++){
    if( _kC == _arr[i]){
      return i;
    }
  }
    return -1;
}