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 have a little bit of fault; when I turn potentiometer in some value , leds get blink ( I think this is because the potentiometer will not get just one parameter and It varies between values with an error, please fix this #define F_CPU 16000000UL // 16 MHz clock
#include <avr/io.h> #include <util/delay.h>
// 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
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();
while(1) { // Read the value from the potentiometer and average it uint16_t potValue = adc_read_average(potPin);
// 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) { // threshold5 PORTD |= (1<<led1) | (1<<led2) | (1<<led3) | (1<<led4) | (1<<led5) | (1<<led6); } else if (mappedValue >= 683) { // threshold4 PORTD |= (1<<led1) | (1<<led2) | (1<<led3) | (1<<led4) | (1<<led5); } else if (mappedValue >= 512) { // threshold3 PORTD |= (1<<led1) | (1<<led2) | (1<<led3) | (1<<led4); } else if (mappedValue >= 341) { // threshold2 PORTD |= (1<<led1) | (1<<led2) | (1<<led3); } else if (mappedValue >= 170) { // threshold1 PORTD |= (1<<led1) | (1<<led2); } else { // Ensure led1 is always turned on regardless of the value PORTD |= (1<<led1); }
// Small delay to reduce flickering _delay_ms(30); } }
- Assistant
The blinking or flickering issue you're experiencing is indeed likely due to the variability in the potentiometer reading. Small fluctuations in the potentiometer's position can cause the input value to hover around the thresholds that determine how many LEDs are lit, which leads to the LEDs turning on and off rapidly.
- User
please give me the full code
- Assistant
- User
with above code I still get led blinking in 6 leds ( I mean when i turn potentiometer in certain degree , the previous led get turned off and next led start to blinking until I turn more the potentiometer and blinking led will turned on
- Assistant
- User
When I want to compile the code, I get this error : main.c: In function ‘main’: main.c:66:12: warning: implicit declaration of function ‘abs’ [-Wimplicit-function-declaration] if(abs(potValue - last_potValue) > HYSTERESIS) { ^
- Assistant
- User
give me the full corrected code
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.