Jesse
Jesse

Reputation: 11

Teensy 4.1 Not Working with OnePinCapSense Library

When verifying my sketch in Arduino, I get the following error:

d:\Documents\Arduino\libraries\OnePinCapSense-master\OnePinCapSense.cpp: In member function 'int OnePinCapSense::readCapacitivePin(int)':
d:\Documents\Arduino\libraries\OnePinCapSense-master\OnePinCapSense.cpp:58:6: error: cannot convert 'volatile uint32_t* {aka volatile long unsigned int*}' to 'volatile uint8_t* {aka volatile unsigned char*}' in assignment
  ddr = portModeRegister(port); // get the ddr 
      ^
d:\Documents\Arduino\libraries\OnePinCapSense-master\OnePinCapSense.cpp:59:8: error: cannot convert 'volatile uint32_t* {aka volatile long unsigned int*}' to 'volatile uint8_t* {aka volatile unsigned char*}' in assignment
  pinIn = portInputRegister(port); // port input register
        ^
d:\Documents\Arduino\libraries\OnePinCapSense-master\OnePinCapSense.cpp:60:9: error: cannot convert 'volatile uint32_t* {aka volatile long unsigned int*}' to 'volatile uint8_t* {aka volatile unsigned char*}' in assignment
  pinOut = portOutputRegister(port);
         ^

exit status 1

Compilation error: exit status 1

I am using a library called OnePinCapSense because I need to use a lot of capacative sensors so using the standard method requirining two pins isn't an option. Below is the sketch in Arduino:

#include "OnePinCapSense.h"

//input pins
int capSensePin2   =  2;
int capSensePin3   =  3;
int capSensePin4   =  4;
int capSensePin5   =  5;

// baseLine is the maximum quiescent value for all sensor pins.
// It will be determined by the calibrate function below.
int baseLine = 0 ;

// offset will be added to the baseLine to provide a value
// to determine when a sensor is touched.
const int offset = 50 ;

// after calibration, touched will be set by adding offset to the 
// calibrated baseLine
int touched = 0 ;

// Cap sense values returned from the library.
// There is a seperate value for each sensor to allow for
// Multi-touch
int capSense2 ;
int capSense3 ;
int capSense4 ;
int capSense5 ;

// create an instance of OnePinCapSense class
OnePinCapSense opcs = OnePinCapSense();

int calibrate()
{
  int sample = 0 ;
  
  for(int i = 2 ; i < 11 ; i+=2)
  {
     // take 30 samples and return the highest value for the pin
     for( int j = 0 ; j < 30 ; j++ )
     {
       sample = opcs.readCapacitivePin(i) ;
       if( sample > baseLine)
       {
         baseLine = sample ;
       }
     }
  }
  touched = baseLine + offset ;
  return touched;
} // end of calibrate

void setup() 
{
  Serial.begin(9600);
  calibrate() ;

  // give some time to bring up the serial monitor
  delay(1000) ;
  
  Serial.print("Baseline: ") ;
  Serial.println(baseLine) ;
  
  Serial.print("Offset: ") ;
  Serial.println(offset) ;
  
  Serial.print("Touched: ") ;
  Serial.println(touched) ;
}

void loop() {
  capSense2 = opcs.readCapacitivePin(capSensePin2) ;
  if( capSense2 > touched)
  {
    Serial.print("Pin2 On \n");

  }
  else
  {

  }
  
  capSense3 = opcs.readCapacitivePin(capSensePin3) ;
  if( capSense3 > touched)
  {
    Serial.print("Pin3 On \n");

  }
  else
  {
    
  }
  
  capSense4 = opcs.readCapacitivePin(capSensePin4) ;
  if( capSense4 > touched)
  {
    Serial.print("Pin4 On");
  }
  else
  {
  
  }
  
  capSense5 = opcs.readCapacitivePin(capSensePin5) ;
  if( capSense5 > touched)
  {
    Serial.print("Pin5 On");
  }
  else
  {
 
  }
  
}

This is just a test to get the capacative sensors working. Now onto the problem. In the file "OnePinCapSense.cpp" I can't figure out how to convert "int pin" into a uint32_t. I think this is the format needed by the functions that reference it. I tried including "pins_arduino.h" into the CapSense file because I read that it can convert the offending functions. Here is the "OnePinCapSense.cpp":

/*
  OnePinCapSense.cpp - Capacitive sensor library that uses a single
                       digital input pin per sensor and allows
                       for detection of multiple simultaneous
                       activations           
  Written by Alan Yorinks
  Copyright (c) 2013 Alan Yorinks All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  
  Version .01 August 7, 2013
*/

#include "OnePinCapSense.h"
#include "pins_arduino.h"  


// Constructor
OnePinCapSense::OnePinCapSense()   // default constructor
{
    // initialize variables

   bitmask = 0 ;        // bitmask to access pin
   port    = 0;         // port register
   *ddr    = 0 ;     // data direction register
   *pinIn  = 0 ;     //input data
   *pinOut = 0 ;     //output data 
   cycles  = 16000 ;    // number of sample cycles    
   
}

// alter the number of sampling cycles
void OnePinCapSense::setNumCycles(int newNumCycles)
{
  cycles = newNumCycles ;
}

// read the capacitive value for the given arduino pin
// and return the read value
int OnePinCapSense::readCapacitivePin(int pin)
{
 port = digitalPinToPort(pin); // get the pin's port
 bitmask = digitalPinToBitMask(pin);
 ddr = portModeRegister(port); // get the ddr 
 pinIn = portInputRegister(port); // port input register
 pinOut = portOutputRegister(port);
 int numCycles = cycles ;  

  // Discharge the pin first by setting it low and output
  *pinOut &= ~(bitmask);
  *ddr  |= bitmask;
  delay(1);
  
  // Make the pin an input WITHOUT the internal pull-up on
  *ddr &= ~(bitmask);
  
  // Now see how many cycles it takes to get the pin pulled up
  for(int i = 0; i < numCycles; i++)
  {

    if (*pinIn & bitmask)
    {
      numCycles = i;
      break;
    }
  }

  // Discharge the pin again by setting it low and output
  //  It's important to leave the pins low if you want to 
  //  be able to touch more than 1 sensor at a time - if
  //  the sensor is left pulled high, when you touch
  //  two sensors, your body will transfer the charge between
  //  sensors.
  *pinOut &= ~(bitmask);
  *ddr  |= bitmask;

  return numCycles ;
}

And here is "pins_arduino.h":

/* Teensyduino Core Library
 * http://www.pjrc.com/teensy/
 * Copyright (c) 2017 PJRC.COM, LLC.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * 1. The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * 2. If the Software is incorporated into a build system that allows
 * selection among a list of target devices, then similar target
 * devices manufactured by PJRC.COM must be included in the list of
 * target devices and selectable in the same manner.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef pins_macros_for_arduino_compatibility_h
#define pins_macros_for_arduino_compatibility_h

#include <stdint.h>

// A0-A9 are always digital 14-23, for Arduino compatibility
#define PIN_A0  (14)
#define PIN_A1  (15)
#define PIN_A2  (16)
#define PIN_A3  (17)
#define PIN_A4  (18)
#define PIN_A5  (19)
#define PIN_A6  (20)
#define PIN_A7  (21)
#define PIN_A8  (22)
#define PIN_A9  (23)
static const uint8_t A0 = PIN_A0;
static const uint8_t A1 = PIN_A1;
static const uint8_t A2 = PIN_A2;
static const uint8_t A3 = PIN_A3;
static const uint8_t A4 = PIN_A4;
static const uint8_t A5 = PIN_A5;
static const uint8_t A6 = PIN_A6;
static const uint8_t A7 = PIN_A7;
static const uint8_t A8 = PIN_A8;
static const uint8_t A9 = PIN_A9;

#if defined(__MK20DX128__)
#define PIN_A10 (34)
#define PIN_A11 (35)
#define PIN_A12 (36)
#define PIN_A13 (37)
static const uint8_t A10 = PIN_A10;
static const uint8_t A11 = PIN_A11;
static const uint8_t A12 = PIN_A12;
static const uint8_t A13 = PIN_A13;

#elif defined(__MK20DX256__)
#define PIN_A10 (34)
#define PIN_A11 (35)
#define PIN_A12 (36)
#define PIN_A13 (37)
#define PIN_A14 (40)
#define PIN_A15 (26)
#define PIN_A16 (27)
#define PIN_A17 (28)
#define PIN_A18 (29)
#define PIN_A19 (30)
#define PIN_A20 (31)
static const uint8_t A10 = PIN_A10;
static const uint8_t A11 = PIN_A11;
static const uint8_t A12 = PIN_A12;
static const uint8_t A13 = PIN_A13;
static const uint8_t A14 = PIN_A14;
static const uint8_t A15 = PIN_A15;
static const uint8_t A16 = PIN_A16;
static const uint8_t A17 = PIN_A17;
static const uint8_t A18 = PIN_A18;
static const uint8_t A19 = PIN_A19;
static const uint8_t A20 = PIN_A20;

#elif defined(__MKL26Z64__)
#define PIN_A10 (24)
#define PIN_A11 (25)
#define PIN_A12 (26)
static const uint8_t A10 = PIN_A10;
static const uint8_t A11 = PIN_A11;
static const uint8_t A12 = PIN_A12;

#elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
#define PIN_A10 (64)
#define PIN_A11 (65)
#define PIN_A12 (31)
#define PIN_A13 (32)
#define PIN_A14 (33)
#define PIN_A15 (34)
#define PIN_A16 (35)
#define PIN_A17 (36)
#define PIN_A18 (37)
#define PIN_A19 (38)
#define PIN_A20 (39)
#define PIN_A21 (66)
#define PIN_A22 (67)
#define PIN_A23 (49)
#define PIN_A24 (50)
#define PIN_A25 (68)
#define PIN_A26 (69)
static const uint8_t A10 = PIN_A10;
static const uint8_t A11 = PIN_A11;
static const uint8_t A12 = PIN_A12;
static const uint8_t A13 = PIN_A13;
static const uint8_t A14 = PIN_A14;
static const uint8_t A15 = PIN_A15;
static const uint8_t A16 = PIN_A16;
static const uint8_t A17 = PIN_A17;
static const uint8_t A18 = PIN_A18;
static const uint8_t A19 = PIN_A19;
static const uint8_t A20 = PIN_A20;
static const uint8_t A21 = PIN_A21;
static const uint8_t A22 = PIN_A22;
static const uint8_t A23 = PIN_A23;
static const uint8_t A24 = PIN_A24;
static const uint8_t A25 = PIN_A25;
static const uint8_t A26 = PIN_A26;
#endif

#define LED_BUILTIN   (13)

#define PIN_SPI_SS    (10)
#define PIN_SPI_MOSI  (11)
#define PIN_SPI_MISO  (12)
#define PIN_SPI_SCK   (13)
static const uint8_t SS = 10;
static const uint8_t MOSI = 11;
static const uint8_t MISO = 12;
static const uint8_t SCK = 13;

#define PIN_WIRE_SDA  (18)
#define PIN_WIRE_SCL  (19)
static const uint8_t SDA = 18;
static const uint8_t SCL = 19;

#define PIN_SERIAL_RX (0)
#define PIN_SERIAL_TX (1)


#define NUM_DIGITAL_PINS  CORE_NUM_DIGITAL
#define NUM_ANALOG_INPUTS CORE_NUM_ANALOG


#define NOT_AN_INTERRUPT -1


#if defined(__MK20DX128__)
  #define analogInputToDigitalPin(p) (((p) <= 9) ? (p) + 14 : (((p) <= 13) ? (p) + 24 : -1))
  #define digitalPinHasPWM(p) (((p) >= 3 && (p) <= 6) || (p) == 9 || (p) == 10 || ((p) >= 20 && (p) <= 23))
  #define digitalPinToInterrupt(p)  ((p) < NUM_DIGITAL_PINS ? (p) : -1)
#elif defined(__MK20DX256__)
  #define analogInputToDigitalPin(p) (((p) <= 9) ? (p) + 14 : (((p) <= 13) ? (p) + 24 : (((p) == 14) ? 40 : (((p) <= 20) ? (p) + 11 : -1))))
  #define digitalPinHasPWM(p) (((p) >= 3 && (p) <= 6) || (p) == 9 || (p) == 10 || ((p) >= 20 && (p) <= 23) || (p) == 25 || (p) == 32)
  #define digitalPinToInterrupt(p)  ((p) < NUM_DIGITAL_PINS ? (p) : -1)
#elif defined(__MKL26Z64__)
  #define analogInputToDigitalPin(p) (((p) <= 9) ? (p) + 14 : (((p) <= 12) ? (p) + 14 : -1))
  #define digitalPinHasPWM(p) ((p) == 3 || (p) == 4 || (p) == 6 || (p) == 9 || (p) == 10 || (p) == 16 || (p) == 17 || (p) == 20 || (p) == 22 || (p) == 23)
  #define digitalPinToInterrupt(p)  ((((p) >= 2 && (p) <= 15) || ((p) >= 20 && (p) <= 23)) ? (p) : -1)
#elif defined(__MK64FX512__)
  #define analogInputToDigitalPin(p) (((p) <= 9) ? (p) + 14 : (((p) >= 12 && (p) <= 20) ? (p) + 19 : (((p) == 23 || (p) == 24) ? (p) + 26 : -1)))
  #define digitalPinHasPWM(p) (((p) >= 2 && (p) <= 10) || (p) == 14 || ((p) >= 20 && (p) <= 23) || (p) == 29 || (p) == 30 || ((p) >= 35 && (p) <= 38))
  #define digitalPinToInterrupt(p)  ((p) < NUM_DIGITAL_PINS ? (p) : -1)
#elif defined(__MK66FX1M0__)
  #define analogInputToDigitalPin(p) (((p) <= 9) ? (p) + 14 : (((p) >= 12 && (p) <= 20) ? (p) + 19 : (((p) == 23 || (p) == 24) ? (p) + 26 : -1)))
  #define digitalPinHasPWM(p) (((p) >= 2 && (p) <= 10) || (p) == 14 || (p) == 16 || (p) == 17 || ((p) >= 20 && (p) <= 23) || (p) == 29 || (p) == 30 || ((p) >= 35 && (p) <= 38))
  #define digitalPinToInterrupt(p)  ((p) < NUM_DIGITAL_PINS ? (p) : -1)
#endif

#define digitalPinToPCICR(p)    ((volatile uint8_t *)0)
#define digitalPinToPCICRbit(p) (0)
#define digitalPinToPCIFR(p)    ((volatile uint8_t *)0)
#define digitalPinToPCIFRbit(p) (0)
#define digitalPinToPCMSK(p)    ((volatile uint8_t *)0)
#define digitalPinToPCMSKbit(p) (0)


#if defined(KINETISK)
struct digital_pin_bitband_and_config_table_struct {
        volatile uint32_t *reg;
        volatile uint32_t *config;
};
extern const struct digital_pin_bitband_and_config_table_struct digital_pin_to_info_PGM[];
// compatibility macros
#define digitalPinToPort(pin) (pin)
#define digitalPinToBitMask(pin) (1)
#define portOutputRegister(pin) ((volatile uint8_t *)(digital_pin_to_info_PGM[(pin)].reg + 0))
#define portSetRegister(pin)    ((volatile uint8_t *)(digital_pin_to_info_PGM[(pin)].reg + 32))
#define portClearRegister(pin)  ((volatile uint8_t *)(digital_pin_to_info_PGM[(pin)].reg + 64))
#define portToggleRegister(pin) ((volatile uint8_t *)(digital_pin_to_info_PGM[(pin)].reg + 96))
#define portInputRegister(pin)  ((volatile uint8_t *)(digital_pin_to_info_PGM[(pin)].reg + 128))
#define portModeRegister(pin)   ((volatile uint8_t *)(digital_pin_to_info_PGM[(pin)].reg + 160))
#define portConfigRegister(pin) ((volatile uint32_t *)(digital_pin_to_info_PGM[(pin)].config))
#define digitalPinToPortReg(pin) (portOutputRegister(pin))
#define digitalPinToBit(pin) (1)

#elif defined(KINETISL)
struct digital_pin_bitband_and_config_table_struct {
        volatile uint8_t *reg;
        volatile uint32_t *config;
    uint8_t mask;
};
extern const struct digital_pin_bitband_and_config_table_struct digital_pin_to_info_PGM[];
// compatibility macros
#define digitalPinToPort(pin) (pin)
#define digitalPinToBitMask(pin) (digital_pin_to_info_PGM[(pin)].mask)
#define portOutputRegister(pin) ((digital_pin_to_info_PGM[(pin)].reg + 0))
#define portSetRegister(pin)    ((digital_pin_to_info_PGM[(pin)].reg + 4))
#define portClearRegister(pin)  ((digital_pin_to_info_PGM[(pin)].reg + 8))
#define portToggleRegister(pin) ((digital_pin_to_info_PGM[(pin)].reg + 12))
#define portInputRegister(pin)  ((digital_pin_to_info_PGM[(pin)].reg + 16))
#define portModeRegister(pin)   ((digital_pin_to_info_PGM[(pin)].reg + 20))
#define portConfigRegister(pin) ((digital_pin_to_info_PGM[(pin)].config))
#define digitalPinToPortReg(pin) (portOutputRegister(pin))
//#define digitalPinToBit(pin) (1)

#endif



#define NOT_ON_TIMER 0
static inline uint8_t digitalPinToTimer(uint8_t) __attribute__((always_inline, unused));
static inline uint8_t digitalPinToTimer(uint8_t pin)
{
    if (pin >= 3 && pin <= 6) return pin - 2;
    if (pin >= 9 && pin <= 10) return pin - 4;
    if (pin >= 20 && pin <= 23) return pin - 13;
    return NOT_ON_TIMER;
}

// These serial port names are intended to allow libraries and architecture-neutral
// sketches to automatically default to the correct port name for a particular type
// of use. For example, a GPS module would normally connect to SERIAL_PORT_HARDWARE_OPEN,
// the first hardware serial port whose RX/TX pins are not dedicated to another use.
//
// SERIAL_PORT_MONITOR Port which normally prints to the Arduino Serial Monitor
//
// SERIAL_PORT_USBVIRTUAL Port which is USB virtual serial
//
// SERIAL_PORT_LINUXBRIDGE Port which connects to a Linux system via Bridge library
//
// SERIAL_PORT_HARDWARE Hardware serial port, physical RX & TX pins.
//
// SERIAL_PORT_HARDWARE_OPEN Hardware serial ports which are open for use. Their RX & TX
// pins are NOT connected to anything by default.
//
#if F_CPU >= 20000000 && !defined(USB_DISABLED)
#define SERIAL_PORT_MONITOR     Serial
#else
#define SERIAL_PORT_MONITOR     Serial1
#endif
#define SERIAL_PORT_USBVIRTUAL      Serial
#define SERIAL_PORT_HARDWARE        Serial1
#define SERIAL_PORT_HARDWARE1       Serial2
#define SERIAL_PORT_HARDWARE2       Serial3
#define SERIAL_PORT_HARDWARE_OPEN   Serial1
#define SERIAL_PORT_HARDWARE_OPEN1  Serial2
#define SERIAL_PORT_HARDWARE_OPEN2  Serial3
#if defined(__MK64FX512__) || defined(__MK66FX1M0__)
#define SERIAL_PORT_HARDWARE3       Serial4
#define SERIAL_PORT_HARDWARE4       Serial5
#define SERIAL_PORT_HARDWARE5       Serial6
#define SERIAL_PORT_HARDWARE_OPEN3  Serial4
#define SERIAL_PORT_HARDWARE_OPEN4  Serial5
#define SERIAL_PORT_HARDWARE_OPEN5  Serial6
#endif

#define SerialUSB           Serial

#endif

Thank you

edit: I realised I forgot to include the "OnePinCapSense.h"

/*
  OnePinCapSense.h - Capacitive sensor library that uses a single
                     digital input pin per sensor and allows
                     for detection of multiple simultaneous
                     activations           
  Written by Alan Yorinks
  Copyright (c) 2013 Alan Yorinks All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  
  Version .01 August 7, 2013
*/

#ifndef ONE_PIN_CAP_SENSE_H
#define ONE_PIN_CAP_SENSE_H

#include <Arduino.h>
#include <pins_arduino.h>

// This class provides the facility to read the value of an input
// pin setup as a capacitve sensor.

// The library requires only one pin per sensor
// and when multiple sensors are used, multi-touch can
// be implemented (see OnePinCapSenseExample.h and see OnePinCapSenseExample.cpp)
// included in this library.


class OnePinCapSense
{
   public:
   // constructors
   OnePinCapSense() ; // default
  
   // methods
   // read the capacitive value for a given pin and return
   // the number of cycles it takes to charge the pin                                    
   int readCapacitivePin( int pin ) ; 
   
   // change the number of sampling cycles
   void setNumCycles(int newNumCycles) ;
   
   private:
   // variables
   uint8_t bitmask ;               // bitmask to access pin
   uint8_t port ;                  // port register
   volatile uint8_t *ddr ;         // data direction register
   volatile uint8_t *pinIn ;       //input data
   volatile uint8_t *pinOut  ;     //output data 
   int cycles ;                    // maximum number of cycles to allow
                                   // pin to charge up.
                                  
   
};

#endif                                         

Upvotes: 0

Views: 112

Answers (0)

Related Questions