Hi, i am kinda new to coding and i've been trying to accomplish a project but it just won't work well.
The code below is for a "lamp". Basically, it uses a touch sensor as a toggle or a switch for the RGB LED, which will change from blue to red and viceversa depending on the LDR light intensity value (if its dark, it turns red, if its bright, it turns blue). Up until now, it works well.
The problem lies when i try to add the third sensor, the infrared proximity sensor. What i want to accomplish is that; when its dark (valorLuz < 400), then it goes from red to orange depending if the infrared sensor detects an obstacle or not, and, when its bright, it goes from blue to green depending if there's an obstacle or not.
I've tried to add it as a separate void and call it in the loop, and also putting it just in the loop, but it just doesn't work. The touch sensor toggle stops working well and the RGB changes color wrong. I don't know what I'm doing wrong?
I'll put below both codes; with and without the infrared sensor, and if someone has advice then it'll be much appreciated.
Without infrared:
/*
Utiliza los siguientes sensores:
- Touch Sensor Module --- Sensor Touch
- Sensor Infrarrojo -- Detectar obstáculos
- Módulo LDR -- Mide la intensidad de la Luz
Microcontrolador:
- Arduino Uno R4
*/
const int ObstaclePin = 2; // Input pin del OUT pin
const int LDRpin = A0; // Pin OUT del sensor LDR
const int touchSensorPin = 3; // pin del sensor touch sensor
const int LED = 6; // LED que se prende cuando el sistema está prendido
const int rledPin = 7; // red RGB LED pin
const int gledPin = 9; // yellow RGB LED pin
const int bledPin = 8; // green RGB LED pin
int lastTouchState = LOW; // estado previo del sensor touch
int currentTouchState; // estado actual del sensor touch
bool ledsAreOn = false; // para rastrear el estado del LED
unsigned long lastDebounceTime = 0; // última vez que fue alternado
unsigned long debounceDelay = 50; // tiempo de antirebote
void setup() {
Serial.begin(9600); // iniciar serial
pinMode(touchSensorPin, INPUT); // configurar los pines
pinMode(ObstaclePin, INPUT);
pinMode(LDRpin, INPUT);
pinMode(rledPin, OUTPUT);
pinMode(gledPin, OUTPUT);
pinMode(bledPin, OUTPUT);
pinMode(LED, OUTPUT);
currentTouchState = digitalRead(touchSensorPin);
}
void loop() {
if (debounceTouchSensor()) {
ledsAreOn = !ledsAreOn;
}
if (ledsAreOn) {
int valorLuz = analogRead(LDRpin);
if (valorLuz < 400) {
red();
}
else if (valorLuz > 400) {
blue();
}
}
else {
digitalWrite(rledPin, LOW);
digitalWrite(gledPin, LOW);
digitalWrite(bledPin, LOW);
}
Serial.println(analogRead(LDRpin));
}
bool debounceTouchSensor() {
int reading = digitalRead(touchSensorPin);
if (reading != lastTouchState) {
lastDebounceTime = millis(); // Reiniciar el temporizador de antirebote
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != currentTouchState) {
currentTouchState = reading;
if (currentTouchState == HIGH) {
lastTouchState = reading;
return true; // Indicar que el sensor touch fue presionado
}
}
}
lastTouchState = reading;
return false;
}
void red(){
digitalWrite(rledPin, HIGH);
digitalWrite(gledPin, LOW);
digitalWrite(bledPin, LOW);
}
void orange(){
digitalWrite(rledPin, HIGH);
digitalWrite(gledPin, HIGH);
digitalWrite(bledPin, LOW);
}
void blue(){
digitalWrite(rledPin, LOW);
digitalWrite(gledPin, LOW);
digitalWrite(bledPin, HIGH);
}
void green(){
digitalWrite(rledPin, LOW);
digitalWrite(gledPin, HIGH);
digitalWrite(bledPin, LOW);
}
With infrared:
/*
Utiliza los siguientes sensores:
- Touch Sensor Module --- Sensor Touch
- Sensor Infrarrojo -- Detectar obstáculos
- Módulo LDR -- Mide la intensidad de la Luz
Microcontrolador:
- Arduino Uno R4
*/
const int ObstaclePin = 2; // Input pin del OUT pin
const int LDRpin = A0; // Pin OUT del sensor LDR
const int touchSensorPin = 3; // pin del sensor touch sensor
const int LED = 6; // LED que se prende cuando el sistema está prendido
const int rledPin = 7; // red RGB LED pin
const int gledPin = 9; // yellow RGB LED pin
const int bledPin = 8; // green RGB LED pin
int lastTouchState = LOW; // estado previo del sensor touch
int currentTouchState; // estado actual del sensor touch
bool ledsAreOn = false; // para rastrear el estado del LED
unsigned long lastDebounceTime = 0; // última vez que fue alternado
unsigned long debounceDelay = 50; // tiempo de antirebote
int Obstacle = digitalRead(ObstaclePin);
void setup() {
Serial.begin(9600); // iniciar serial
pinMode(touchSensorPin, INPUT); // configurar los pines
pinMode(ObstaclePin, INPUT);
pinMode(LDRpin, INPUT);
pinMode(rledPin, OUTPUT);
pinMode(gledPin, OUTPUT);
pinMode(bledPin, OUTPUT);
pinMode(LED, OUTPUT);
currentTouchState = digitalRead(touchSensorPin);
}
void loop() {
if (debounceTouchSensor()) {
ledsAreOn = !ledsAreOn;
}
if (ledsAreOn) {
int valorLuz = analogRead(LDRpin);
if (valorLuz < 400) { // Hay mucha luz
morning();
}
else if (valorLuz > 400) { // Hay poca luz
night();
}
}
else {
digitalWrite(rledPin && gledPin && bledPin, LOW);
}
Serial.println(analogRead(LDRpin));
}
bool debounceTouchSensor() {
int reading = digitalRead(touchSensorPin);
if (reading != lastTouchState) {
lastDebounceTime = millis(); // Reiniciar el temporizador de antirebote
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != currentTouchState) {
currentTouchState = reading;
if (currentTouchState == HIGH) {
lastTouchState = reading;
return true; // Indicar que el sensor touch fue presionado
}
}
}
lastTouchState = reading;
return false;
}
void red(){ // Luz roja
digitalWrite(rledPin, HIGH);
digitalWrite(gledPin, LOW);
digitalWrite(bledPin, LOW);
}
void orange(){ // Luz naranja
digitalWrite(rledPin, HIGH);
digitalWrite(gledPin, HIGH);
digitalWrite(bledPin, LOW);
}
void blue(){ // Luz azul
digitalWrite(rledPin, LOW);
digitalWrite(gledPin, LOW);
digitalWrite(bledPin, HIGH);
}
void green(){ // Luz verde
digitalWrite(rledPin, LOW);
digitalWrite(gledPin, HIGH);
digitalWrite(bledPin, LOW);
}
void night(){ // Poca luz == de noche
if(Obstacle == LOW){ // Si hay un obstáculo == da luz naranja
orange();
}
else if(Obstacle == HIGH){ // No hay un obstáculo == da luz roja
red();
}
}
void morning(){ // Mucha luz == de día
if(Obstacle == LOW){ // Si hay un obstáculo == da luz verde
green();
}
else if(Obstacle == HIGH){ // No hay un obstáculo == da luz azul
blue();
}
}