This shows you the differences between two versions of the page.
pm:prj2023:tmiu:soundactivatedlights [2023/05/27 12:53] denisa.stanescu |
pm:prj2023:tmiu:soundactivatedlights [2023/05/30 12:28] (current) denisa.stanescu |
||
---|---|---|---|
Line 46: | Line 46: | ||
* ADC (lab4) | * ADC (lab4) | ||
- | - sensorValue = (float)analogRead(sensorPin) * (5.0 / 1024.0); | + | -sensorValue = (float)analogRead(sensorPin) * (5.0 / 1024.0); |
- | - performs an analog-to-digital conversion using the analogRead function; it reads the analog value from sensorPin and converts it to a digital value | + | -performs an analog-to-digital conversion using the analogRead function; it reads the analog value from sensorPin and converts it to a digital value |
* Timer (lab3) | * Timer (lab3) | ||
- | - Timer1.initialize(100000); | + | -Timer1.initialize(100000); |
- | - sets the timer interval to 100ms (0.1s); this allows for periodic execution of a function based on the timer interval | + | -sets the timer interval to 100ms (0.1s); this allows for periodic execution of a function based on the timer interval |
* Interrupt (lab2) | * Interrupt (lab2) | ||
- | - Timer1.attachInterrupt(FlashLEDs); | + | -Timer1.attachInterrupt(FlashLEDs); |
- | - attaches the FlashLEDs function to the interrupt triggered by Timer1; this means that every time the timer interrupt occurs (every 100ms), the FlashLEDs function will be executed. | + | -attaches the FlashLEDs function to the interrupt triggered by Timer1; this means that every time the timer interrupt occurs (every 100ms), the FlashLEDs function will be executed. |
* libraries and 3rd party sources: | * libraries and 3rd party sources: | ||
- | - <avr/interrupt.h> | + | -<avr/interrupt.h> |
- | - TimerOne.h (It is used in this code to set up a timer interrupt for flashing the LEDs) | + | -TimerOne.h (It is used in this code to set up a timer interrupt for flashing the LEDs) |
* algorithms and structures: | * algorithms and structures: | ||
- | - MainFunction(): Reads the sensor value, filters the signal, and compares it to predefined values to determine the color to display on the RGB LED strip | + | -MainFunction(): Reads the sensor value, filters the signal, and compares it to predefined values to determine the color to display on the RGB LED strip |
- | - FilterSignal(float sensorSignal): Applies a filter to the sensor signal using a weighted moving average algorithm | + | -FilterSignal(float sensorSignal): Applies a filter to the sensor signal using a weighted moving average algorithm |
- | - CompareSignalFiltered(float filteredSignal): Compares the filtered signal to predefined thresholds to determine the appropriate color to display | + | -CompareSignalFiltered(float filteredSignal): Compares the filtered signal to predefined thresholds to determine the appropriate color to display |
- | - RGBColor(int Rcolor, int Gcolor, int Bcolor): Sets the color of the RGB LED strip by writing appropriate values to the Rpin, Gpin, and Bpin pins | + | -RGBColor(int Rcolor, int Gcolor, int Bcolor): Sets the color of the RGB LED strip by writing appropriate values to the Rpin, Gpin, and Bpin pins |
* functions: | * functions: | ||
- | - setup(): Sets up the initial configuration of the program, including serial communication initialization and timer setup | + | -setup(): Sets up the initial configuration of the program, including serial communication initialization and timer setup |
- | - loop(): Contains the main execution loop, calling the MainFunction() repeatedly | + | -loop(): Contains the main execution loop, calling the MainFunction() repeatedly |
- | - FlashLEDs(): A timer interrupt service routine (ISR) that alternates the LED strip between white and off states | + | -FlashLEDs(): A timer interrupt service routine (ISR) that alternates the LED strip between white and off states |
+ | * | ||
+ | <file c++> | ||
+ | |||
+ | #include <avr/interrupt.h> | ||
+ | #include <TimerOne.h> | ||
+ | |||
+ | #define Rpin 11 | ||
+ | #define Gpin 10 | ||
+ | #define Bpin 9 | ||
+ | #define delayLEDS 3 | ||
+ | #define sensorPin A0 | ||
+ | |||
+ | float sensorValue = 0, filteredSignal = 0, | ||
+ | filteredSignalValues[] = {3.4, 3.1, 2.7, 2.4, 2.1, 1.7, 1.3, 0.9, 0.4}; | ||
+ | |||
+ | void setup() { | ||
+ | Serial.begin(9600); | ||
+ | |||
+ | Timer1.initialize(100000); // Set the timer interval to 100ms (0.1s) | ||
+ | Timer1.attachInterrupt(FlashLEDs); // Attach the FlashLEDs function to the timer interrupt | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | MainFunction(); | ||
+ | } | ||
+ | |||
+ | void MainFunction() { | ||
+ | sensorValue = (float)analogRead(sensorPin) * (5.0 / 1024.0); | ||
+ | FilterSignal(sensorValue); | ||
+ | Serial.print(sensorValue); | ||
+ | Serial.print(" "); | ||
+ | Serial.println(filteredSignal); | ||
+ | CompareSignalFiltered(filteredSignal); | ||
+ | } | ||
+ | |||
+ | void FilterSignal(float sensorSignal) { | ||
+ | filteredSignal = (0.945 * filteredSignal) + (0.0549 * sensorSignal); | ||
+ | } | ||
+ | |||
+ | 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 RGBColor(int Rcolor, int Gcolor, int Bcolor) { | ||
+ | analogWrite(Rpin, Rcolor); | ||
+ | analogWrite(Gpin, Gcolor); | ||
+ | analogWrite(Bpin, Bcolor); | ||
+ | |||
+ | delay(delayLEDS); | ||
+ | } | ||
+ | |||
+ | void FlashLEDs() { | ||
+ | static boolean flashState = false; | ||
+ | | ||
+ | if (flashState) { | ||
+ | RGBColor(255, 255, 255); // White color | ||
+ | flashState = false; | ||
+ | } else { | ||
+ | RGBColor(0, 0, 0); // Turn off LEDs | ||
+ | flashState = true; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </file> | ||
Line 104: | Line 201: | ||
===== Download ===== | ===== Download ===== | ||
+ | {{:pm:prj2023:tmiu:proj_details.zip|}} | ||
- | O arhivă (sau mai multe dacă este cazul) cu fişierele obţinute în urma realizării proiectului: surse, scheme, etc. Un fişier README, un ChangeLog, un script de compilare şi copiere automată pe uC crează întotdeauna o impresie bună ;-). | + | ===== Jurnal ===== |
- | Fişierele se încarcă pe wiki folosind facilitatea **Add Images or other files**. Namespace-ul în care se încarcă fişierele este de tipul **:pm:prj20??:c?** sau **:pm:prj20??:c?:nume_student** (dacă este cazul). **Exemplu:** Dumitru Alin, 331CC -> **:pm:prj2009:cc:dumitru_alin**. | ||
+ | Finished initial doc. (7/5/2023) | ||
- | ===== Jurnal ===== | + | Added hardware scheme. (28/5/2023) |
+ | |||
+ | Added software program.(28/05/2023) | ||
- | Puteți avea și o secțiune de jurnal în care să poată urmări asistentul de proiect progresul proiectului. | ||
+ | ===== Bibliography ===== | ||
- | ===== Bibliografie/Resurse ===== | ||
- | Listă cu documente, datasheet-uri, resurse Internet folosite, eventual grupate pe **Resurse Software** şi **Resurse Hardware**. | + | [[https://www.geeksforgeeks.org/introduction-of-led/]] |
+ | [[https://randomnerdtutorials.com/guide-for-ws2812b-addressable-rgb-led-strip-with-arduino/]] | ||
<html><a class="media mediafile mf_pdf" href="?do=export_pdf">Export to PDF</a></html> | <html><a class="media mediafile mf_pdf" href="?do=export_pdf">Export to PDF</a></html> | ||