Bonjour,
Le code que j'essai de faire fonctionner consiste à créer une séquence de "toucher" avec le doigt d'une PIN, lorsque que la séquence est correcte, un aimant se désactive.
Mais j'ai l'impression que lorsque je touche une pin elle ne se détecte pas, hors les led fonctionnent bien lors du touché (elle s'allume lorsque je touche la PIN et s'éteint quand je ne touche plus la PIN)
Voici le code :
#include <CapacitiveSensor.h>
#include <Adafruit_NeoPixel.h>
#include <SoftwareSerial.h>
#define DEBUG
#define NUMPIXELS 24
#define ledPins 8
#define bouton1 2
#define bouton2 6
const byte numInputs = 2;
const byte inputPins[numInputs] = {2, 6};
const byte numSteps = 5;
const byte steps[numSteps] = {0, 1, 0, 0, 1};
const byte lockPin = A5;
bool lastInputState[] = {HIGH, HIGH, HIGH, HIGH};
int currentStep = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
CapacitiveSensor capSensor = CapacitiveSensor(4, 2);
int threshold = 300;
int threshold2 = 300;
Adafruit_NeoPixel pixels(NUMPIXELS, ledPins, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(9600);
pinMode(ledPins, OUTPUT);
pinMode(bouton1, INPUT_PULLUP);
pinMode(bouton2, INPUT_PULLUP);
for(int i=0; i< numInputs; i++){
pinMode(inputPins[i], INPUT_PULLUP);
}
pinMode(lockPin, OUTPUT);
digitalWrite(lockPin, LOW);
#ifdef DEBUG
Serial.begin(9600);
Serial.println(F("Serial communication started"));
#endif
}
void loop() {
long sensorValue = capSensor.capacitiveSensor(30);
Serial.println(sensorValue);
if (sensorValue > threshold) {
for (int i = 0; i <= NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(255, 165, 0));
pixels.show();
}
}
else {
for (int i = 0; i <= NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
pixels.show();
}
if (sensorValue > threshold2) {
digitalWrite(ledPins, HIGH);
}
else {
digitalWrite(ledPins, LOW);
}
delay(10);
}
if ( (millis() - lastDebounceTime) > debounceDelay) {
for(int i=0; i<numInputs; i++){
int currentInputState = digitalRead(inputPins[i]);
if(currentInputState != lastInputState[i]) {
lastDebounceTime = millis();
}
if(currentInputState == LOW && lastInputState[i] == HIGH) {
if(steps[currentStep] == i) {
currentStep++;
#ifdef DEBUG
Serial.print(F("Correct input! Onto step #"));
Serial.println(currentStep);
#endif
}
else {
currentStep = 0;
Serial.println(F("Incorrect input! Back to the beginning!"));
}
}
lastInputState[i] = currentInputState;
}
}
if(currentStep == numSteps){
onSolve();
}
}
void onSolve(){
#ifdef DEBUG
Serial.println(F("Puzzle Solved!"));
#endif
digitalWrite(lockPin, HIGH);
delay(300);
digitalWrite(lockPin, LOW);
}
Merci pour votre aide !