#define OC1A PB5
#define OC3A PE3
#define OC4A PH3
volatile uint16_t redValue = 0;
volatile uint16_t greenValue = 0;
volatile uint16_t blueValue = 0;
volatile float adcValue;
volatile float redValue_buf=0;
volatile float greenValue_buf=0;
volatile float blueValue_buf=0;
ISR(ADC_vect) // Interruption de conversion ADC
{adcValue =ADC;
adcValue = (adcValue/1024); // Lire la valeur ADC
// Serial.println(adcValue);
// // Serial.print("ADC Value: ");
// // Serial.println(adcValue);
// OCR1A = redValue*adcValue;
// OCR3A = greenValue*adcValue;
// OCR4A = blueValue*adcValue;
// OCR1A = redValue*adcValue;
// OCR3A = greenValue*adcValue;
// OCR4A = blueValue*adcValue;
// // Mettre à jour les valeurs PWM en fonction de la valeur ADC
// redValue = adcValue * 1023 / 1023; // Exemple de mise à jour, ajustez selon vos besoins
// greenValue = adcValue * 1023 / 1023;
// blueValue = adcValue * 1023 / 1023;
// OCR1A = redValue;
// OCR3A = greenValue;
// OCR4A = blueValue;
if (OCR1A < 2)
OCR1A = 2; // Set OCR1A to 1 if it is 0
if (OCR3A< 2)
OCR3A = 2; // Set OCR3A to 1 if it is 0
if (OCR4A < 2)
OCR4A = 2; // Set OCR4A to 1 if it is 0
ADCSRA |= (1 << ADSC); // Démarrer une nouvelle conversion
}
void port_ini(void)
{
PORTB = 0x00; // Clear PORTB
DDRB = (1 << OC1A); // Set PB5 as output
DDRE = (1 << OC3A); // Set PE3 as output
DDRH = (1 << OC4A); // Set PH3 as output
}
void pwm_timer_init(void)
{
// Timer 1 configuration for red channel
TCCR1A = (1 << COM1A1) | (0 << COM1A0) /* Clear OCA on Compare Match, set OCA on BOTTOM (non-inverting mode) */
| (0 << COM1B1) | (0 << COM1B0) /* Normal port operation, OCB disconnected */
| (1 << WGM11) | (1 << WGM10); /* TC16 Mode 7 Fast 10-bit PWM */
TCCR1B = (0 << WGM13) | (1 << WGM12) /* TC16 Mode 7 Fast 10-bit PWM */
| 0 << ICNC1 /* Input Capture Noise Canceler: disabled */
| 0 << ICES1 /* Input Capture Edge Select: disabled */
| (0 << CS12) | (1 << CS11) | (1 << CS10); /* IO clock divided by 64 */
OCR1A = 1; /* Output compare A: 0x64 */
TIMSK1 = 0 << OCIE1B /* Output Compare B Match Interrupt Enable: disabled */
| 1 << OCIE1A /* Output Compare A Match Interrupt Enable: enabled */
| 0 << ICIE1 /* Input Capture Interrupt Enable: disabled */
| 1 << TOIE1; /* Overflow Interrupt Enable: enabled */
// Timer 3 configuration for green channel
TCCR3A = (1 << COM3A1) | (0 << COM3A0) /* Clear OCA on Compare Match, set OCA on BOTTOM (non-inverting mode) */
| (0 << COM3B1) | (0 << COM3B0) /* Normal port operation, OCB disconnected */
| (1 << WGM31) | (1 << WGM30); /* TC16 Mode 7 Fast 10-bit PWM */
TCCR3B = (0 << WGM33) | (1 << WGM32) /* TC16 Mode 7 Fast 10-bit PWM */
| 0 << ICNC3 /* Input Capture Noise Canceler: disabled */
| 0 << ICES3 /* Input Capture Edge Select: disabled */
| (0 << CS32) | (1 << CS31) | (1 << CS30); /* IO clock divided by 64 */
OCR3A = 1; /* Output compare A: 0x64 */
TIMSK3 = 0 << OCIE3B /* Output Compare B Match Interrupt Enable: disabled */
| 1 << OCIE3A /* Output Compare A Match Interrupt Enable: enabled */
| 0 << ICIE3 /* Input Capture Interrupt Enable: disabled */
| 1 << TOIE3; /* Overflow Interrupt Enable: enabled */
// Timer 4 configuration for blue channel
TCCR4A = (1 << COM4A1) | (0 << COM4A0) /* Clear OCA on Compare Match, set OCA on BOTTOM (non-inverting mode) */
| (0 << COM4B1) | (0 << COM4B0) /* Normal port operation, OCB disconnected */
| (1 << WGM41) | (1 << WGM40); /* TC16 Mode 7 Fast 10-bit PWM */
TCCR4B = (0 << WGM43) | (1 << WGM42) /* TC16 Mode 7 Fast 10-bit PWM */
| 0 << ICNC4 /* Input Capture Noise Canceler: disabled */
| 0 << ICES4 /* Input Capture Edge Select: disabled */
| (0 << CS42) | (1 << CS41) | (1 << CS40); /* IO clock divided by 64 */
OCR4A = 1; /* Output compare A: 0x64 */
TIMSK4 = 0 << OCIE4B /* Output Compare B Match Interrupt Enable: disabled */
| 1 << OCIE4A /* Output Compare A Match Interrupt Enable: enabled */
| 0 << ICIE4 /* Input Capture Interrupt Enable: disabled */
| 1 << TOIE4; /* Overflow Interrupt Enable: enabled */
}
void Init_ADC()
{
ADMUX = (1 << REFS0) | // Référence de tension = VCC (5V)
(1 << MUX0); // Entrée ADC sur AD1
ADCSRA = (1 << ADEN) | // Activer l'ADC
(1 << ADSC) | // Démarrer la première conversion
(1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0) | // Prédiviseur sur 128
(1 << ADIE); // Activer l'interruption ADC
}
void setColor(const char *color)
{
long number = strtol(color, NULL, 16);
redValue = ((number >> 16) & 0xFF) * 1023 / 255;
greenValue = ((number >> 8) & 0xFF) * 1023 / 255;
blueValue = (number & 0xFF) * 1023 / 255;
OCR1A = redValue*adcValue;
OCR3A = greenValue*adcValue;
OCR4A = blueValue*adcValue;
if (OCR1A < 2)
OCR1A = 2; // Set OCR1A to 1 if it is 0
if (OCR3A< 2)
OCR3A = 2; // Set OCR3A to 1 if it is 0
if (OCR4A < 2)
OCR4A = 2; // Set OCR4A to 1 if it is 0
Serial.print("Red Value: ");
Serial.println(OCR1A);
Serial.print("Green Value: ");
Serial.println(OCR3A);
Serial.print("Blue Value: ");
Serial.println(OCR4A);
}
void setup()
{
Serial.begin(115200);
sei();
pwm_timer_init(); // Initialize PWM timer
Init_ADC();
port_ini(); // Initialize ports
// Example command
// setColor("FF0000"); // Set red color
// setColor("00FF00"); //Set green color
// setColor("0000FF"); // Set blue color
}
void loop()
{
if (Serial.available() > 0)
{
String color = Serial.readStringUntil('\n'); // Read the color sent via the serial port
setColor(color.c_str()); // Update the color
}
_delay_ms(250);
}
ISR(TIMER1_OVF_vect)
{
OCR1A = redValue*adcValue;
OCR3A = greenValue*adcValue;
OCR4A = blueValue*adcValue;
// Handle Timer 1 overflow interrupt
}
ISR(TIMER1_COMPA_vect)
{
// Handle Timer 1 compare match A interrupt
}
ISR(TIMER3_OVF_vect)
{
// Handle Timer 3 overflow interrupt
}
ISR(TIMER3_COMPA_vect)
{
// Handle Timer 3 compare match A interrupt
}
ISR(TIMER4_OVF_vect)
{
// Handle Timer 4 overflow interrupt
}
ISR(TIMER4_COMPA_vect)
{
// Handle Timer 4 compare match A interrupt
}
int main(void)
{
setup();
Serial.println("Введите число в формате RRGGBB");
while (1)
{
if (Serial.available() > 0)
{
String input = Serial.readStringUntil('\n'); // Read the string until newline
// Check the length of the string
if (input.length() == 6)
{
long color = strtol(input.c_str(), NULL, 16); // Convert the string to a color value
setColor(input.c_str());
}
else
{
// Display an error message
Serial.println("Ошибка ввода!");
_delay_ms(500);
Serial.println("Введите число в формате RRGGBB");
}
}
}
}