#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Motor to Left most side 30 and right most side 155 and centre 92
int center = 92;
int left = 30;
int right = 155;
volatile int motor_direction = center;
float Temp_Ch_Val[2] = {0};
int channel = 0;
bool swing = false;
int countbig = 10;
int countsmall = 5;
ISR(ADC_vect)
{
Temp_Ch_Val[channel] = 1/(log(1/(1023./ADC-1))/3950+1.0/298.15)-273.15;
channel++;
if (channel == 2) {
channel = 0;
}
ADMUX = (1 << REFS0) | channel;
// Start next Conversion
ADCSRA |= (1 << ADSC);
}
ISR(TIMER0_COMPA_vect)
{
OCR0A = motor_direction;
}
ISR(INT0_vect)
{
if(swing)
swing = false;
else
swing = true;
}
void setup() {
lcd.init();
lcd.backlight();
DDRD = ~(1 << PD2);
DDRD |= (1 << PD6);
DDRC = 0x00;
DDRB = 0x00;
DDRD |= (1<< PD0);
PORTD |= (1 << PD2);
EIMSK = (1 << INT0);
sei();
ADMUX = (1 << REFS0); // reference voltage = AVCC
ADCSRA |= (1 << ADIE) | (1<< ADEN) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
// Start the first conversion
// Configure Timer0 for fast PWM mode with prescaler = 8
TCCR0A = (1 << WGM01) | (1 << WGM00) | (1 << COM0A1);
TCCR0B = (1 << CS02);
OCR0A = 92;
// Enable Timer0 compare match A interrupt and global interrupts
TIMSK0 = (1 << OCIE0A);
}
void loop() {
lcd.setCursor(0, 0);
lcd.print("T1:");
lcd.print(Temp_Ch_Val[1]);
lcd.print("T2:");
lcd.print(Temp_Ch_Val[0]);
lcd.setCursor(0, 1);
if(swing)
{
lcd.print("Sw ON ");
ADCSRA |= (1 << ADSC);
PORTD &= ~(1 << PD0);
if(Temp_Ch_Val[1] > Temp_Ch_Val[0])
{
motor_direction = left;
if(countbig > 0)
{
lcd.print(" Left ");
countbig--;
_delay_ms(200);
lcd.print(countbig);
countsmall = 5;
}
else
{
motor_direction = right;
lcd.print(" Right ");
countsmall--;
lcd.print(countsmall);
if(countsmall == 0)
countbig = 10;
_delay_ms(200);
}
}
else if(Temp_Ch_Val[0] > Temp_Ch_Val[1])
{
motor_direction = right;
if(countbig > 0)
{
lcd.print(" Right ");
countbig--;
_delay_ms(200);
lcd.print(countbig);
countsmall = 5;
}
else
{
motor_direction = left;
lcd.print(" Left ");
countsmall--;
lcd.print(countsmall);
if(countsmall == 0)
countbig = 10;
_delay_ms(200);
}
}
else if(Temp_Ch_Val[1] = Temp_Ch_Val[0])
{
motor_direction = center;
lcd.print(" Center ");
}
}
else
{
lcd.print("Sw OFF");
PORTD |= (1 << PD0);
if((PINB & (1 << PB5)))
{
motor_direction = left;
lcd.print(" Left ");
}
else if ((PINB & (1 << PB4)))
{
motor_direction = right;
lcd.print(" Right ");
}
else
{
motor_direction = center;
lcd.print(" Center ");
}
}
}