Hi all, recently i've been experimenting with different sensors. i've noticed something strange. first let me explain my connections and parts.
Here are what I'm using:
1- Arduino Wemos D1 mini
2- a general digital touch sensor (capacitive)
3- 2-pin LED
4- 20x4 LCD + LCD I2C TWI SP Interface
5- 4-pin push button (ON/OFF LCD backlight)
also attached a Fritzing board sketch.
here is my simplified code :
//======================================================================================================
// LIBRARIES
//======================================================================================================
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <OneWire.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//======================================================================================================
// VARIABLES
//======================================================================================================
const int buttonPin = 14; // Backlight Buton Pin -- D0
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
const IPAddress ipa(192, 168, 1, 200);
const IPAddress gate(192, 168, 1, 1);
const IPAddress sub(255, 255, 255, 0);
const char* unitnum = "1";
const char* ssid = "---";
const char* password = "---";
int timeinterval = 1000;
unsigned long timecheck = 0;
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
unsigned long currentMillis = 0;
unsigned long tspreviousMillis = 0;
unsigned long bllaststat = 0;
boolean lcdStatus = true;
boolean lcdbacklightStatus = true;
boolean tscurrentState = LOW;
boolean tsState;
boolean tslastState = LOW;
boolean ledState = LOW;
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 500; // the debounce time; increase if the output flickers
int ledpin = 0; // LED Pin -- D3
int tspin = 12; // Touch Sensor Pin -- D6
ESP8266WebServer server(84);
WiFiClient client;
//======================================================================================================
// SETUP
//======================================================================================================
void setup() {
lcd.begin(20,4); // initialize the lcd
lcd.clear();
lcd.print("Initializing... ");
Wire.begin();
Serial.begin(115200);
toggleLEDoff();
toggleBoardLEDoff();
WiFi.mode(WIFI_STA);
WiFi.config(ipa, gate, sub);
WiFi.begin(ssid, password);
Serial.println("");
//Pin Allocation
pinMode(ledpin, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT); // Onboard LED Pin
pinMode(tspin, INPUT);
pinMode(buttonPin, INPUT);
lcd.clear();
lcd.print("Connecting to Wifi");
delay(2000);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
toggleLEDoff();
toggleBoardLEDoff();
//If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
lcd.clear();
lcd.print("Wifi connected. ");
lcd.print("IP: ");
lcd.print(WiFi.localIP());
delay(2000);
//Initialize Webserver
server.on("/",[](){
server.send(200, "text/plain", "Wemos D1 mini Server is Online.");
});
server.on("/ledon",[](){
toggleLEDon();
server.send(200, "text/plain", "LED Turned On.");
});
server.on("/ledoff",[](){
toggleLEDoff();
server.send(200, "text/plain", "LED Turned Off.");
});
server.on("/ledtg",[](){
toggleLED();
server.send(200, "text/plain", "LED Turned Toggle.");
});
server.onNotFound([](){server.send(404, "text/plain", "404: Not found");});
server.begin();
Serial.println("HTTP server started");
lcd.clear();
}
//======================================================================================================
// LOOP
//======================================================================================================
void loop() {
server.handleClient();
currentMillis = millis();
if (currentMillis - bllaststat > 180000 && lcdbacklightStatus){
lcdbacklightStatus = false;
turnoffLcdbacklight();
bllaststat = currentMillis;
}
updatebutton();
if (currentMillis - previousMillis3 >= 200) {
previousMillis3 = currentMillis;
updateTS();
}
}
//======================================================================================================
// FUNCTIONS
//======================================================================================================
//===============================================================
// LED PIN 16 ON/OFF/TOGGLE
//===============================================================
void toggleLEDon(){if(digitalRead(LDRpin) == LOW){digitalWrite(ledpin, HIGH);}}
void toggleLED(){digitalWrite(ledpin, !digitalRead(ledpin));}
void toggleLEDoff(){if(digitalRead(LDRpin) == HIGH){digitalWrite(ledpin, LOW);}}
//===============================================================
// LED ONBOARD ON/OFF/TOGGLE
//===============================================================
void toggleBoardLEDon(){if(!digitalRead(LED_BUILTIN) == LOW){digitalWrite(LED_BUILTIN, LOW);}}
void toggleBoardLED(){digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));}
void toggleBoardLEDoff(){if(!digitalRead(LED_BUILTIN) == HIGH){digitalWrite(LED_BUILTIN, HIGH);}}
//===============================================================
// LCD BACKLIGHT ON/OFF
//===============================================================
void turnoffLcdbacklight(){ lcd.noBacklight(); lcdbacklightStatus = false;}
void turnonLcdbacklight(){ lcd.backlight(); lcdbacklightStatus = true;}
void toggleLcdbacklight(){
if (lcdbacklightStatus){
lcd.noBacklight();
}else{
lcd.backlight();
}
bllaststat = currentMillis;
lcdbacklightStatus = !lcdbacklightStatus;
}
//===============================================================
// LCD BACKLIGHT TOGGLE BUTTON
//===============================================================
void updatebutton(){
if (digitalRead(buttonPin) == HIGH) {
toggleLcdbacklight();
delay(500);
}
}
//===============================================================
// TOUCH SENSOR
//===============================================================
void updateTS(){
// Touch Sensor Start
tscurrentState = digitalRead(tspin);
if (tscurrentState != tslastState) { lastDebounceTime = currentMillis; }
if ((currentMillis - lastDebounceTime) >= debounceDelay) {
if (tscurrentState != tsState) {
tsState = tscurrentState;
if (tsState == HIGH) {
toggleLED();
}
}
}
tslastState = tscurrentState;
}
so strange thing is, whenever i push the button, i does the job it suppose to, but then touch sensor lit up and toggle the LED on/off. i'm really confused.
could someone please explain whats going on?
thanks in advance.