0% found this document useful (0 votes)
31 views

Led H

This Arduino code defines pin assignments for an RGB LED and analog sensor. It takes sensor readings, filters the signal, compares it to thresholds to determine the color, and displays that color on the RGB LED. Pushing a button toggles the sensor reading and LED display on and off.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Led H

This Arduino code defines pin assignments for an RGB LED and analog sensor. It takes sensor readings, filters the signal, compares it to thresholds to determine the color, and displays that color on the RGB LED. Pushing a button toggles the sensor reading and LED display on and off.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

const int buttonPin = 2; // número do pino pushbutton

#define Rpin 11
#define Gpin 10
#define Bpin 9
#define delayLEDS 3
#define sensorPin A0

void RGBColor(int Rcolor, int Gcolor, int Bcolor) {

analogWrite(Rpin, Rcolor);
analogWrite(Gpin, Gcolor);
analogWrite(Bpin, Bcolor);

delay(delayLEDS);
}

float sensorValue = 0, filteredSignal = 0,


filteredSignalValues[] = {3.4, 3.1, 2.7, 2.4, 2.1, 1.7, 1.3, 0.9, 0.4};

int estado = 0; // variável para leitura do pushbutton


int guarda_estado = LOW; // variável para armazenar valores do pushbutton

void CompareSignalFiltered(float filteredSignal) {

if (filteredSignal > filteredSignalValues[0]) {


RGBColor(0, 0, 255);
Serial.println("Blue");
} else if (filteredSignal <= filteredSignalValues[0] && filteredSignal >
filteredSignalValues[1]) {
Serial.println("Azure");
RGBColor(0, 255, 255);
} else if (filteredSignal <= filteredSignalValues[1] && filteredSignal >
filteredSignalValues[2]) {
RGBColor(0, 127, 255);
Serial.println("Cyan");
} else if (filteredSignal <= filteredSignalValues[2] && filteredSignal >
filteredSignalValues[3]) {
RGBColor(0, 255, 127);
Serial.println("Aqua marine");
} else if (filteredSignal <= filteredSignalValues[3] && filteredSignal >
filteredSignalValues[4]) {
RGBColor(0, 255, 0);
Serial.println("Green");
} else if (filteredSignal <= filteredSignalValues[4] && filteredSignal >
filteredSignalValues[5]) {
RGBColor(255, 255, 0);
Serial.println("Yellow");
} else if (filteredSignal <= filteredSignalValues[5] && filteredSignal >
filteredSignalValues[6]) {
RGBColor(255, 0, 255);
Serial.println("Magenta");
} else if (filteredSignal <= filteredSignalValues[6] && filteredSignal >
filteredSignalValues[7]) {
RGBColor(255, 0, 127);
Serial.println("Rose");
} else if (filteredSignal <= filteredSignalValues[7] && filteredSignal >
filteredSignalValues[8]) {
RGBColor(255, 127, 0);
Serial.println("Orange");
} else if (filteredSignal <= filteredSignalValues[8]) {
RGBColor(255, 0, 0);
Serial.println("Red");
} else {
RGBColor(0, 0, 255);
Serial.println("Default: Blue");
}}

void FilterSignal(float sensorSignal) {

filteredSignal = (0.945 * filteredSignal) + (0.0549 * sensorSignal);


}

void MainFunction() {

sensorValue = (float) analogRead(sensorPin) * (5.0 / 1024.0);

FilterSignal(sensorValue);

Serial.print(sensorValue);
Serial.print(" ");
Serial.println(filteredSignal);

CompareSignalFiltered(filteredSignal);
}

void setup() {
Serial.begin (9600);
pinMode(buttonPin, INPUT); // utilize INPUT_PULLUP com resistor interno do Arduino
}

void loop(){
// le o estado pushbutton: ligado (HIGH) ou desligado (LOW)
estado = digitalRead(buttonPin); // le o estado do push button HIGH ou LOW

if (estado== HIGH) {
guarda_estado = !guarda_estado; // inverte valor da variável guarda_estado
while (!digitalRead(buttonPin)) { } //espera soltar o botão
delay(50);
}

if (guarda_estado == HIGH) {
MainFunction();
}
else if (guarda_estado == LOW){
Serial.print("Off");
RGBColor(100,0,0);
}
}

You might also like