I want to design a board to light up 6 LEDs(like LED bar), based on Values of A 10K Ohm…
- User
I want to design a board to light up 6 LEDs(like LED bar), based on Values of A 10K Ohm Potentiometer which I read from a power LED driver board, I have the code below : #define F_CPU 16000000UL // 16 MHz clock
#include <avr/io.h> #include <util/delay.h>
// Define potentiometer ADC channel (PA0 = ADC0) #define potPin 0
// Define digital I/O pins for LEDs - changed to use 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 ADMUX = (1<<REFS0); // Select AVcc as reference voltage and ADC0 as input channel ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); // Enable ADC and set prescaler to 128 }
uint16_t adc_read(uint8_t ch) { // Select ADC channel with safety mask and without changing the reference voltage selection 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 - updated for PD1 to PD6 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);
code// Map the potentiometer value from the given range (65 - 337) to (0 - 1023) uint16_t mappedValue = (uint32_t)(potValue - 65) * 1023 / (337 - 65); // Define thresholds based on the number of LEDs uint16_t threshold1 = 170; // First threshold uint16_t threshold2 = 341; // Second threshold uint16_t threshold3 = 512; // Third threshold uint16_t threshold4 = 683; // Fourth threshold uint16_t threshold5 = 854; // Fifth threshold // Turn off all LEDs to start with - updated for PD1 to PD6 PORTD &= ~((1<<led1) | (1<<led2) | (1<<led3) | (1<<led4) | (1<<led5) | (1<<led6)); // Determine which LEDs to light up based on the mappedValue - updated for PD1 to PD6 if (mappedValue >= threshold5) { PORTD |= (1<<led1) | (1<<led2) | (1<<led3) | (1<<led4) | (1<<led5) | (1<<led6); } else if (mappedValue >= threshold4) { PORTD |= (1<<led1) | (1<<led2) | (1<<led3) | (1<<led4) | (1<<led5); } else if (mappedValue >= threshold3) { PORTD |= (1<<led1) | (1<<led2) | (1<<led3) | (1<<led4); } else if (mappedValue >= threshold2) { PORTD |= (1<<led1) | (1<<led2) | (1<<led3); } else if (mappedValue >= 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
It seems like the issue is with the threshold values you have defined for turning on the LEDs. The threshold values should be adjusted based on the range of the potentiometer values you are getting.
Currently, the threshold values you have defined are: uint16_t threshold1 = 170; // First threshold uint16_t threshold2 = 341; // Second threshold uint16_t threshold3 = 512; // Third threshold uint16_t threshold4 = 683; // Fourth threshold uint16_t threshold5 = 854; // Fifth threshold
Based on your description, it seems like when you reduce the value of the potentiometer, all LEDs are turned on because the mapped value falls below threshold1. This suggests that the threshold values are not appropriate for the range of values you are getting from the potentiometer.
- User
give me the full corrected code :
- Assistant
- User
I copied this code, but still when potantioumeter at its max all 6 leds are turned on and and when I reduce the value of potentiometer, did not turned off all LED's until i hit the minimum value and than just led one(as it suppose to be) turned on
- Assistant
- User
OK but I read the potentiometer value on the LED driver board and at minimum it shows at minimum 65 and in max shows 337
- Assistant
I apologize for the confusion.
In that case, it seems that the mapping equation in the code is correct. Please make sure that the connections between the microcontroller and the LED driver board are correct and check the potentiometer on the LED driver board to ensure it is functioning properly.
If the connections and the potentiometer are correct, it's possible that there may be an issue with the LED driver board itself. You may want to double-check the specifications and documentation of the LED driver board or consult the manufacturer for further assistance.
Additionally, you can try adding some debug statements to your code to print out the potentiometer value and the mapped value to the serial monitor. This can help you verify if the values are being read and mapped correctly by the microcontroller.