Need Help with capacitive touch lamp project

Hey,

I need some help with my code for my LED (Neopixel) lamp.
It has one capacitive touch button, wich should turn the Neopixels on/off, if taped ones and change the brightness if you hold him. I'm kinda new, it's the first time using "millis" for me.

thanks for any kind of help!

#include <CapacitiveSensor.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 10

Adafruit_NeoPixel strip = Adafruit_NeoPixel(4, PIN, NEO_GRB + NEO_KHZ800);
CapacitiveSensor   cs_4_2 = CapacitiveSensor(2, 4);
int long_tap = false;
int short_tap = false;
unsigned long previousMillis = 0;
const long interval = 1000;

void setup()
{
  cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);
  Serial.begin(9600);
  strip.begin();
  strip.show();
}

void loop()
{
  long sensor1 =  cs_4_2.capacitiveSensor(30);
  unsigned long currentMillis = millis();
  if (sensor1 > 1000) {
    if (currentMillis - previousMillis >= interval)
    {
      previousMillis = currentMillis;
      if (long_tap == true)
      {
        strip.setPixelColor(0, 255, 255, 255);
      }
      else
      {
        short_tap = false;
      }
    }
  }
}

I added serial debugoutput to your code that makes visible what is happening inside the code.

it uses a macro for this. macros are no C++-functions. They are pre-compile-"functions"

So run this code to analyse what you code is doing right know what is missing an dmust be added

#include <CapacitiveSensor.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 10

//#define debugTxtVar(abc, def) Serial.print("<  "#abc" = "); Serial.print(def); Serial.println('>')

// using the #define "statement" to make the compiler "write" code
// #define can be used to make the compiler replace text that describes something with (almost) anything else
// easy example 
// let's assume IO-pin number 4 shall be set to work as output 
// the command for defining an IO-pin to be configured as output is
//
// pinMode(4,OUTPUT); 
// the number "4" says nothing about he purpose of this IO-pin

// let's assume the IO-pin shall switch On/Off a buzzer

// you would have to add a comment to explain
// pinMode(4,OUTPUT); // IO-pin 4 is buzzer

// the compiler needs the number. To make the code easier to read and understand
// #define BuzzerPin 4
// So the command looks like this
// pinMode(BuzzerPin,OUTPUT);

// the descriptive word "BuzzerPin" gets replaced through the compiler by a "4"
// so what the compiler compiles is still 'pinMode(4,OUTPUT);'

// the #define can do much more than just replace a word by a single number
// it can even take parameters like in this example

#define debugTxtVar(myParameterText, variableName) \
        Serial.print(#myParameterText " " #variableName"="); \
        Serial.println(variableName); 


Adafruit_NeoPixel strip = Adafruit_NeoPixel(4, PIN, NEO_GRB + NEO_KHZ800);
CapacitiveSensor   cs_4_2 = CapacitiveSensor(2, 4);
int long_tap = false;
int short_tap = false;
unsigned long previousMillis = 0;
const long interval = 1000;

void setup()
{
  cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);
  Serial.begin(9600);
  strip.begin();
  strip.show();
}

void loop() {
  delay(200); // remove this delay for final use
  // it is just inserted to reduce the speed of the serial-output
  long sensor1 =  cs_4_2.capacitiveSensor(30);
  unsigned long currentMillis = millis();
  
  if (sensor1 > 1000) {
    debugTxtVar("sensor1 > 1000)",sensor1);
    
    if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
      debugTxtVar("interval is over",long_tap);      
      if (long_tap == true) {
        strip.setPixelColor(0, 255, 255, 255);
      }
      else {
        short_tap = false;
        debugTxtVar("long_tap is false",long_tap);      
        debugTxtVar("long_tap is false",short_tap);      
      }
    }
  }
}

best regards Stefan

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.