#include <avr/io.h>
#include <util/delay.h>
void setup() {
// Set PD2, PD3, and PD4 as output pins
DDRD |= (1 << 2);
DDRD |= (1 << 3);
DDRD |= (1 << 4);
// Set PD5, PD6, and PD7 as input pins
DDRD &= ~(1 << 5);
DDRD &= ~(1 << 6);
DDRD &= ~(1 << 7);
// ADC setup
ADMUX = 0; // Select ADC0
ADMUX &= ~(1 << MUX0); // Ensure it's using ADC0
ADCSRA |= (1 << ADEN) | (1 << ADSC); // Enable ADC and start conversion
// Initialize serial communication
Serial.begin(9600);
}
void butanLesen(double *x)
{
double butan=0;
ADCSRA |= (1<<ADSC);
butan = ADCL|ADCH<<8;
_delay_ms(200);
double y=(50/1023);
*x=butan*1000/1023;
}
void Ampel(double x) {
if (x >= 100 && x < 500) {
// Green light on, others off
PORTD &= ~(1 << 3);
PORTD &= ~(1 << 4);
PORTD |= (1 << 2);
_delay_ms(600);
}
if (x >= 500 && x < 700) {
// Yellow light on, others off
PORTD &= ~(1 << 2);
PORTD &= ~(1 << 4);
PORTD |= (1 << 3);
_delay_ms(600);
}
if (x >= 900) {
// Red light on, others off
PORTD &= ~(1 << 2);
PORTD &= ~(1 << 3);
PORTD |= (1 << 4);
_delay_ms(600);
}
// Check if button on PD7 is pressed and x is 3 (reset condition)
if (PIND & (1 << 7)) {
// All lights off
PORTD &= ~(1 << 2);
PORTD &= ~(1 << 3);
PORTD &= ~(1 << 4);
_delay_ms(600);
}
}
int main() {
// Initial setup
setup();
double x = 0;
while (1) {
// Read sensor value
butanLesen(&x);
Ampel(x);
}
return 0;
}