#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h> //Adafruit library
// Define OLED display
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
uint8_t ledBright1;
uint8_t ledBright2;
uint8_t ledBright3;
uint8_t ldrVal1;
uint8_t ldrVal2;
uint8_t ldrVal3;
ISR(ADC_vect) {
if (ADCSRA &= ~(1 << ADSC)) { // if ADSC is LOW then read ADC
switch(ADMUX){
case 0x40: {
ldrVal1 = ADC;
//Serial.println(ldrVal1);
ADMUX = 0x41;
break;}
case 0x41: {
ldrVal2= ADC;
ADMUX = 0x42;
break;}
case 0x42: {
ldrVal3 = ADC;
ADMUX = 0x40;
break;}}
}
ADCSRA |= (1 << ADSC); // Start the next ADC conversion
}
//OCR1A ISR
ISR(TIMER1_COMPA_vect)
{
OCR1A = ledBright1; // Yellow LED connected on PIN9
}
// OCR1B ISR
ISR(TIMER1_COMPB_vect)
{
OCR1B = ledBright2; // Yellow LED connected on PIN10
}
uint8_t ldr2brightness(uint8_t ldr)
{
return (ldr * 0.2492 + 1); // (255/1023) to convert 10-bit ADC to 8-bit PWM
}
void setup() {
//Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Please press the");
display.println("blue button to");
display.println("Turn ON");
display.display();
// Set Up ADC with prescalar 128
ADCSRA = (1 << ADEN) | (1 << ADIE) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
ADCSRA |= (1 << ADSC); // Conversion start
ADMUX = 0x40;
// Set up Timer1 PWM on OCR1A and OCR1B to control brightness of 2 Yellow LEDs
// Mode-5 operation (TOP-0xFF-255) with Non-Inv Fast PWM
TCCR1A |= (1 << COM1A1) | (1 << COM1B1) | (1 << WGM10);
TCCR1B |= (1 << WGM12) | (1 << CS12); // Prescalar-256
TIMSK1 |= (1 << OCIE1A); // For CompA OCR1A interrupt
TIMSK1 |= (1 << OCIE1B); // For CompB OCR1B interrupt
DDRB |= (1 << PB1);
PORTB |= (1 << PB1);
DDRB |= (1 << PB2);
PORTB |= (1 << PB2);
}
void loop() {
PORTB |= (1 << PB2);
delay(500);
PORTB &= ~(1 << PB2);
delay(500);
ledBright1 = ldr2brightness(ldrVal1);
}