3 sensors, 3 leds issue

Hello, I’m wondering if anyone can help with this? I have 3 capacitance sensors triggering 3 leds. What should happen is that each sensor should trigger its respective led, that is, the led is held high while the finger remains on the sensor and then switches low when the finger isn’t. With the present code when sensor 3 is pressed the led remains on until 2 is pressed which remains on until 1 is pressed ans so on. Can anyone highlight any obvious errors I’ve made? Thanks in advance

#include <CapacitiveSensor.h>

CapacitiveSensor cs[] = {
  CapacitiveSensor(2, 38),
  CapacitiveSensor(2, 39),
  CapacitiveSensor(2, 40)
};
const int numberOfSensors = 3;
const int ledPin[numberOfSensors] = {22, 23, 24};
const int threshold = 200;

void setup()
{
  Serial.begin(9600);
  for (int i = 0; i < numberOfSensors; i++) {
    pinMode(ledPin[i], OUTPUT);
  }
}

void loop()
{
  for (int i = 0; i < numberOfSensors; i++)
  {
    static boolean lastSensorHit = false;
    bool sensorHit = cs[i].capacitiveSensor(30) > threshold;

    if (sensorHit != lastSensorHit)
      if (sensorHit && !lastSensorHit)
      {
        digitalWrite(ledPin[i], HIGH);
      }
      else {
        digitalWrite(ledPin[i], LOW);
      }
    lastSensorHit = sensorHit;
  }
}

You don’t need your lastSensorHit value if you simply want the LED to match the finger press. Just read the sensor and if HIGH, turn on LED else turn off LED. Loop over all sensors

void loop()
{
  for (int i = 0; i < numberOfSensors; i++) {
    bool sensorHit = cs[i].capacitiveSensor(30) > threshold;
    if (sensorHit) {
      digitalWrite(ledPin[i], HIGH);
    }
    else {
      digitalWrite(ledPin[i], LOW);
    }
  }
}

You can also improve your code by letting the compiler figure out how many sensors you have

//const int numberOfSensors = 3;
const int numberOfSensors = sizeof(cs) / sizeof(cs[0])

Then, when you add another sensor, the code still works.

Absolutely stunning! Works perfectly!! Thanks a million blh64!!

And for anyone who will find this usefull…

#include <CapacitiveSensor.h>

CapacitiveSensor cs[] = {
  CapacitiveSensor(2, 38),
  CapacitiveSensor(2, 39),
  CapacitiveSensor(2, 40)
};
//const int numberOfSensors = 3;
const int numberOfSensors = sizeof(cs) / sizeof(cs[0]);
const int ledPin[numberOfSensors] = {22, 23, 24};
const int threshold = 200;

void setup()
{
  Serial.begin(9600);
  for (int i = 0; i < numberOfSensors; i++) {
    pinMode(ledPin[i], OUTPUT);
  }
}

void loop()
{
  for (int i = 0; i < numberOfSensors; i++)
  {
    static boolean lastSensorHit = false;
    bool sensorHit = cs[i].capacitiveSensor(30) > threshold;

    if (sensorHit) {
      digitalWrite(ledPin[i], HIGH);
    }
    else {
      digitalWrite(ledPin[i], LOW);
    }
  }
}

blh64, originally I had this code for sending midi notes and now I've noticed when adding midi back to this present code I have some old issues coming back. When triggering a sensor it continually sends a note like a machine gun. What I had with the old code was one note sent when touching the sensor, sustain whilst finger remains on the sensor and then the note cuts off when removing the finger. That's why I still had these left in

 if (sensorHit1 != lastSensorHit1)
    if (sensorHit1 && !lastSensorHit1)

Can you suggest a workaround for this please?

here’s what I now have with the midi added

#include <CapacitiveSensor.h>
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();

CapacitiveSensor cs[] = {
  CapacitiveSensor(2, 38),
  CapacitiveSensor(2, 39),
  CapacitiveSensor(2, 40)
};
//const int numberOfSensors = 3;
const int numberOfSensors = sizeof(cs) / sizeof(cs[0]);
const int ledPin[numberOfSensors] = {22, 23, 24};
const int threshold = 200;

void setup()
{
  Serial.begin(9600);
  for (int i = 0; i < numberOfSensors; i++) {
    pinMode(ledPin[i], OUTPUT);
  }
}

void loop()
{
  for (int i = 0; i < numberOfSensors; i++)
  {
    static boolean lastSensorHit = false;
    bool sensorHit = cs[i].capacitiveSensor(30) > threshold;

    if (sensorHit) {
        digitalWrite(ledPin[i], HIGH);
        MIDI.sendNoteOn(60, 127, 1);
    MIDI.sendControlChange(64, 127, 1);
      }
      else {
        digitalWrite(ledPin[i], LOW);
         MIDI.sendNoteOff(60, 0, 1);
    MIDI.sendControlChange(64, 0, 1);
      }
  }
}

So you’ve created an entire new set of requirements that were not in the original post. You are destine for Management my friend.

Your new requirement is that you have to know the transition from touch to no-touch, not just the current state. Luckily, this can be done by reading the state of the LED (yes, you can read output pins)

#include <CapacitiveSensor.h>
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();

CapacitiveSensor cs[] = {
  CapacitiveSensor(2, 38),
  CapacitiveSensor(2, 39),
  CapacitiveSensor(2, 40)
};
//const int numberOfSensors = 3;
const int numberOfSensors = sizeof(cs) / sizeof(cs[0]);
const int ledPin[numberOfSensors] = {22, 23, 24};
const int threshold = 200;

void setup()
{
  Serial.begin(9600);
  for (int i = 0; i < numberOfSensors; i++) {
    pinMode(ledPin[i], OUTPUT);
  }
}

void loop()
{
  for (int i = 0; i < numberOfSensors; i++) {

    bool sensorHit = cs[i].capacitiveSensor(30) > threshold;

    if (sensorHit) {
      // see if we are currently hit by testing ledPin state
      if (digitalRead(ledPin[i]) == LOW ) {
        // Just transistioned into this state so do the MIDI thing
        digitalWrite(ledPin[i], HIGH);
        MIDI.sendNoteOn(60, 127, 1);
        MIDI.sendControlChange(64, 127, 1);
      }
    }
    else {
      // sensor not hit so see if this is a new state
      if (digitalRead(ledPin[i]) == HIGH ) {
        // Just transistioned into this state so shut it down
        digitalWrite(ledPin[i], LOW);
        MIDI.sendNoteOff(60, 0, 1);
        MIDI.sendControlChange(64, 0, 1);
      }
    }
  }
}

So you’ve created an entire new set of requirements that were not in the original post. You are destine for Management my friend.

:slight_smile: If I’d only known it was that easy :slight_smile: To be honest, I wanted to pull apart and try to understand all the processes and interactions of a piece of code I had been using up til now, add a few features and reduce all the copy and paste! You’ve fixed the last piece of that puzzle for me thank you kindly blh64! So now she works! I have a pot and some arrays written in for scale selection as well but I want to work on a system of scale creation, minor, majpr etc… Hold that management job for me!

#include <SPI.h>
#include <CapacitiveSensor.h>
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();

int c3 = 36;
int d3 = 38;
int e3 = 40;
int g3 = 43;
int a3 = 45;
int c4 = 48;
int d4 = 50;
int e4 = 52;
int g4 = 55;
int a4 = 57;
int c5 = 60;
int d5 = 62;
int e5 = 64;
int g5 = 67;
int a5 = 69;
int c6 = 72;

const int columns = 3;
const int scales = 3;
int potVal = 0;
const int  notes[scales][columns] = {
  {c3, d3, e3},
  {c4, d4, e4},
  {c5, d5, e5}
};

CapacitiveSensor cs[] = {
  CapacitiveSensor(2, 38),
  CapacitiveSensor(2, 39),
  CapacitiveSensor(2, 40)
};
//const int numberOfSensors = 3;
const int numberOfSensors = sizeof(cs) / sizeof(cs[0]);
const int ledPin[numberOfSensors] = {22, 23, 24};
const int threshold = 200;

void setup()
{
  Serial.begin(9600);
  for (int i = 0; i < numberOfSensors; i++) {
    pinMode(ledPin[i], OUTPUT);
  }
}

void loop()
{
  int potVal = map(analogRead(A2), 0, 1024, 0, 3);
  for (int i = 0; i < numberOfSensors; i++) {
  checkSensor(potVal, i);
  }
}
void checkSensor(int scaleIndex, int columnIndex)
{
    for (int i = 0; i < numberOfSensors; i++) {

    bool sensorHit = cs[i].capacitiveSensor(10) > threshold;

    if (sensorHit) {
      // see if we are currently hit by testing ledPin state
      if (digitalRead(ledPin[i]) == LOW ) {
        // Just transistioned into this state so do the MIDI thing
        digitalWrite(ledPin[i], HIGH);
        MIDI.sendNoteOn(notes[scaleIndex][i], 127, 1);
      MIDI.sendControlChange(64, 127, 1);
      }
    }
    else {
      // sensor not hit so see if this is a new state
      if (digitalRead(ledPin[i]) == HIGH ) {
        // Just transistioned into this state so shut it down
        digitalWrite(ledPin[i], LOW);
        MIDI.sendNoteOff(notes[scaleIndex][i], 0, 1);
      MIDI.sendControlChange(64, 0, 1);
      }
    }
  }
}

Luckily, this can be done by reading the state of the LED (yes, you can read output pins)

Awesome!! :)