I have this code as below which , Turns 6 LED on based on the reading value from 10 k…
- User
I have this code as below which , Turns 6 LED on based on the reading value from 10 k potentiometer of a High power LED driver board; this code works very good but I use a 5 pin potentiometer which have On/Off pins; I attached this POT to where high power led board get its power but, my atmega 32 and 6 leds get their power from else where; I want to adjust this code; so when there is no data from High power led(in analog pins) turns off all the 6 leds (my purpose is when I turn off my high power driver with POT; the 6 leds also turn off) #define F_CPU 16000000UL // 16 MHz clock
#include <avr/io.h> #include <util/delay.h> #include <stdlib.h> // Included for abs()
// Define potentiometer ADC channel (PC0 = ADC0 on ATmega328P) #define potPin 0
// Define digital I/O pins for LEDs - Using PD1 to PD6 #define led1 PD1 #define led2 PD2 #define led3 PD3 #define led4 PD4 #define led5 PD5 #define led6 PD6
#define SAMPLES_TO_AVERAGE 10 // Number of samples to average for ADC #define HYSTERESIS 25 // Hysteresis value to avoid flickering
void adc_init() { // Initialize ADC for ATmega328P ADMUX = (1<<REFS0); // AVcc reference voltage, ADC0 as input ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); // Enable ADC, prescaler to 128 }
uint16_t adc_read(uint8_t ch) { // Select ADC channel with safety mask, for ATmega328P ADMUX = (ADMUX & 0xF0) | (ch & 0x0F);
// Start single conversion ADCSRA |= (1<<ADSC); // Wait until conversion is complete while (ADCSRA & (1<<ADSC)); return ADC;}
uint16_t adc_read_average(uint8_t ch) { uint32_t sum = 0; for (int i = 0; i < SAMPLES_TO_AVERAGE; ++i) { sum += adc_read(ch); } return (uint16_t)(sum / SAMPLES_TO_AVERAGE); }
int main(void) { // Set up the LED pins as output - For ATmega328P DDRD |= (1<<led1) | (1<<led2) | (1<<led3) | (1<<led4) | (1<<led5) | (1<<led6);
// Initialize ADC adc_init(); uint16_t last_potValue = 0xFFFF; // Use 0xFFFF to ensure significant change on first read while(1) { // Read the value from the potentiometer and average it uint16_t potValue = adc_read_average(potPin); // Apply hysteresis by checking if change is beyond a threshold if(abs((int)potValue - (int)last_potValue) > HYSTERESIS) { last_potValue = potValue; // Update the last recorded value // Map the potentiometer value to accommodate the LED thresholds uint16_t mappedValue = (uint32_t)(potValue - 65) * 1023 / (337 - 65); // Turn off all LEDs to start with PORTD &= ~((1<<led1) | (1<<led2) | (1<<led3) | (1<<led4) | (1<<led5) | (1<<led6)); // Determine which LEDs to light up based on the mappedValue if (mappedValue >= 854) { PORTD |= (1<<led1) | (1<<led2) | (1<<led3) | (1<<led4) | (1<<led5) | (1<<led6); } else if (mappedValue >= 683) { PORTD |= (1<<led1) | (1<<led2) | (1<<led3) | (1<<led4) | (1<<led5); } else if (mappedValue >= 512) { PORTD |= (1<<led1) | (1<<led2) | (1<<led3) | (1<<led4); } else if (mappedValue >= 341) { PORTD |= (1<<led1) | (1<<led2) | (1<<led3); } else if (mappedValue >= 170) { PORTD |= (1<<led1) | (1<<led2); } else { // Ensure led1 is always turned on regardless of the value PORTD |= (1<<led1); } } // Small delay to reduce potential flickering _delay_ms(30); } - Assistant
- User
give me the full code
- Assistant
- User
I tested code above; when the high power LED driver board is turned off with POT; the Six LEDs are turned off but after powering on the POT and High power LED driver board just led 1 is turned on and led 2 is blinking; please fix this :
- Assistant
- User
give me the full corrected code
- Assistant
- User
I tested this code , its doing good job to turn off 6 leds but, after turning potentiometer on the led 1 is just turned on and after rotating the POT a little bit more; the led 2 starts blinking and no other leds are turned on ; please fix it
- Assistant
- User
give me the full code
- Assistant
- User
I used your code but , it make it worst, the first led turned on when the pot value was in mid rotation ; please correct it based on explanation i graved to you on above
- Assistant
- User
the code above need more works on how to turn all; the first led is turned on but the second led still need lots of turn and other leds are not turned on; if this could help the below code was the code actually was working uint16_t adc_read_average(uint8_t ch) { uint32_t sum = 0; for (int i = 0; i < SAMPLES_TO_AVERAGE; ++i) { sum += adc_read(ch); } return (uint16_t)(sum / SAMPLES_TO_AVERAGE); }
int main(void) { // Set up the LED pins as output - For ATmega328P DDRD |= (1<<led1) | (1<<led2) | (1<<led3) | (1<<led4) | (1<<led5) | (1<<led6);
- Assistant
- User
is it the full code ?
- Assistant
- User
this code works better than the previous codes but ; first of all it gets the high power LEDs driver and LED flickering and also it dose not work properly , after the POT turned to ON position the all of the 6 leds are turned on and after changing the pot it starts unseal work and some time last LED is not turned on please make it correct
- Assistant
- User
is t the full code ?
- Assistant
- User
- Assistant