Gerriko / PicoCapSense

An Arduino Capacitive Sensing library for RP2040 based boards (uses the PIO processor).
GNU General Public License v3.0
23 stars 3 forks source link

Adafruit Feather Serial communcation with CapSense problem #4

Closed fred-rabelo closed 4 months ago

fred-rabelo commented 4 months ago

hi!

First thanks for the Library!

I am trying to use it in my project but i am getting some issue.

It seems that the Capsense function sometimes make the cpu to stop whe the touch sensor is triggered. Blinking led stops and Serial communication also. Touch the sensor again a few times makes the board work again. But only if connected to the computer. If i power the board with battery or USB charger, it works just fine (at least the blinking led goes on). The problem is that my project is a software controller (a daw fader controller), so it has to be connected to the computer.

does anyone have a similar issue or any ideas where to look for problems?

Apriciate any help!

Board: Adafruit Feather RP2040, Earle Philhower core

code: (sorry for the formatting... the "insert code" button doesnt seem to work properly)

`

include

include

include "blink_led.hpp"

include "Fader.hpp"

void setup() {

Serial.begin(9600); while (!Serial) {;;} // wait for serial port to connect. Needed for native USB //delay(1000);

led_setup(); // Setup function to blink LED for assessing if CPU is running properly fader_setup(); // Setup for the fader

}

void loop() {

blink_led(); // Function to blink LED for assessing if CPU is running properly

update_fader();

} `

`#ifndef BLINK_LED_H

define BLINK_LED_H

void led_setup(); void blink_led();

endif`

`// ------------------------ Blink LED ------------------------------------------

/ Function to blink LED for assessing if CPU is running properly /

include "blink_led.hpp"

include

include

int led = LED_BUILTIN; bool led_status = 0; unsigned long timer_led = 0; // saves last time the Led was

void led_setup () { // set LED to be an output pin

pinMode(led, OUTPUT);

}

void blink_led() // Basic blink function {

const int led_offtime = 200;     // offtime to blink LED in ms
const int led_ontime = 50;     // on time of LED in ms

if (led_status == 0)          // if LED is Turned OFF
{
  if (micros() - timer_led > led_offtime * 1000)  // check if elapsed time is greater than led_offtime (Converted to microseconds) 
  {
    timer_led = micros();         // Update Timer
    led_status = 1;     // Switch led-Status to 1
    digitalWrite(led,led_status);  // Turn LED on    

    // DEBUG
    //Serial.println("LED on");
    //Serial.println(led_status);
  }
}

else if (micros() - timer_led > led_ontime * 1000)  // if LED is ON, checks if elapsed time since LED was turned on is greater than led_ontime (Converted to microseconds) 
{
  led_status = 0;     // Switch led-Status to 0
  digitalWrite(led,led_status);  // Turn LED off

  // DEBUG
  // Serial.println("LED off");
  //Serial.println(led_status);
}

}

// -----------------------------------------------------------------------------

`

`#ifndef fader_h

define fader_h

void fader_setup(); void update_fader ();

void update_fader_pos(); void update_touch(); bool isTouch();

bool calibrate_minmax();

endif`

`#include "Fader.hpp"

include

include

include

include "PicoCapSensing.h"

// -------------------------- Declare and Setup Fader Stuff ------------------------------------

// Initialize Fader

// Define Constants
  const int fader_pin = A0;
  const int adc_res = 10;                                 // analog digital converter resolution in bits _ set up ADC to 10 bit value (pro tools max resolution 0.1 dB)
  const int ADC_MAX_VALUE = (pow(2,adc_res)-1);           // analog digital converter max value (2 raised to "adc_res", -1 to account for value 0)

// Declare "global" variables
  uint fader_pos = 0;
  uint prev_fader_pos = 0;
  uint min_fader = 0;
  uint max_fader = ADC_MAX_VALUE;
  float pos_update_period = 1;      // in ms
  u_long timer_fader = 0;

//

// initialize Touch Sensor

// Define Constants
  const uint touch_update_period = pos_update_period;     // period to read touch sensor in ms
  const int touch_threshold = 15;                         // Value taken from raw readings
  const int touch_samples = 10;                            // number of samples (max 50) taken by "getCapSensingSample"
  const int touch_timeout = 20;                         // timeout in ms when waiting to receive change state by "getCapSensingSample"

  const int touch_pin_receive = A3;     // connected to Fader touch, must be analog pin
  const int touch_pin_send = 25;        // Connected to pin_receive trough 1MΩ resistor, must be digital pin

// Declare "global" variables
  bool touch_status = 0;
  bool prev_touch_status = 0;
  u_long timer_touch = 0;

  float touch_EMA_a = 0.3;      //initialization of EMA alpha
  int touch_EMA_S = 10;         //initialization of EMA S (to "untouched" value of sensor)
  int touch_raw = 0;

PicoPIO capPicoPIO(pio0);
PicoCapSensing CapSensor(capPicoPIO, touch_pin_send, touch_pin_receive);

//

// Setup Fader void fader_setup() {

  pinMode(fader_pin,INPUT);
  pinMode(touch_pin_receive,INPUT);
  analogReadResolution(adc_res);

} 

//

// -------------------------- Declare and Setup Fader Stuff ------------------------------------

// ------------------------------- Update Fader ------------------------------------------------

void update_fader() {

update_fader_pos();
update_touch();

}

// ------------------------------- Update Fader ------------------------------------------------

// ------------------------------- Update Fader Pos --------------------------------------------

  void update_fader_pos()
  {
    if (micros() - timer_fader > (pos_update_period) * 1000)  // convert Period to us
    {
      timer_fader = micros();

      prev_fader_pos = fader_pos;

      fader_pos = analogRead(fader_pin);    

    }
  }

// ------------------------------- Update Fader Pos --------------------------------------------

// ----------------------------- Update Touch Status -------------------------------------------

void update_touch()
{

  if (micros() - timer_touch > (touch_update_period) * 1000)  // check if it is time to update _ convert Period to us
  {

    timer_touch = micros();       // Update Timer
    touch_status = isTouch();     // Update Touch Status

    //Serial.println("touch_status: " + String(touch_status));
    //Serial.print(touch_status);
    //Serial.print(" ");

    if (touch_status)             // If touch is True
    {

      if (touch_status != prev_touch_status) // if Touch is True and was False before, send "Touch on" MIDI message to DAW
      {

        prev_touch_status = touch_status;     // Update last touch status

          //Serial.println("Touch On , Touch status: " + String(touch_status));
          digitalWrite(LED_BUILTIN,1);  // Debug using LED ;
      }

    }                        

    else if (touch_status != prev_touch_status) // if Touch is False and was True before, send MIDI message
    {
      prev_touch_status = touch_status;  // Update last touch status

      // Debug 
        //Serial.println("Touch Off Track: " + String(sel_track + 1));    
        //Serial.println("Touch Off , Touch status: " + String(touch_status));
        digitalWrite(LED_BUILTIN,0);  // Debug using LED ;
    }

  }

}

// ----------------------------- Update Touch Status -------------------------------------------

// --------------------------- Read Touch as True/False ----------------------------------------

bool isTouch()
{

  touch_raw = CapSensor.getCapSensingSample(touch_timeout, touch_samples);

  // Debug Serial
    //Serial.println("Raw touch sensor: " + String(CapSensor.getCapSensingSample(2000, touch_samples)) + ", filtered: " + String(touch_EMA_S));
    //Serial.print(touch_status);
    //Serial.print(" ");
    //Serial.print(CapSensor.getCapSensingSample(2000, touch_samples));
    //Serial.print(" ");
    //Serial.println(touch_EMA_S);

  // Return Touch as Bool
  if (touch_EMA_S > touch_threshold) return true;
  else return false;
}

// --------------------------- Read Touch as True/False ---------------------------------------- `

Gerriko commented 4 months ago

The library.properties file has "architectures=mbed_rp2040", which would show this library as being incompatible with the Earle Philhower core.

You would need to trawl through the PicoCapSensing.cpp file to check what's causing the error as it is probably an mbed function that is the problem.

fred-rabelo commented 4 months ago

oh i see! i believe that would a little beyond my skills now. Thanks for the reply!