I was following a tutorial I found to get a photocell to light an LED as a first step to building a morse coder/decoder. I only have an arduino UNO available instead of the ATmega328P chip itself to work with. I have connected the photocell to pinout A0 and the LED to pinout D~9 on the UNO. I tried to rewrite the code to be used for my current setup but It comes up with three errors and four warnings that I can not figure out how to solve. Any help or advice would be greatly appreciated. I'm using Atmel Studio 7 Gcc C.
#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <avr/io.h>
#include <util/delay.h>
#define PHOTO1 0 //sets photocell to PORTC pinout A0
#define LED1 9 //sets LED to PORTB pinout 9
void init_ports_mcu()
{
DDRB = 0xFFu; //set all pins at PORTB as output
PORTB = 0x00u; // sets pins at PORTB as low - LED off
DDRC = 0xFFu; //sets all pins at PORTC as output
DDRC &= ~(1<<0); //Makes first pin at PORTC input
PORTC = 0x00u; //sets all pins at PORTC as low-turning off Photocell
}
int map(int x, int in_min, int in_max, int out_min, int out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void ADC_init()
{
//Enable ADC sampling freq to set prescaler to max value
ADCSRA |= (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);
ADMUX = (1<<REFS0); //select required channel passed as input
}
uint16_t get_lightLevel()
{
_delay_ms 10; //wait for line in channel to get selected
ADCSRA |= (1<<ADSC); //start ADC conversion
while (ADCSRA & (1<<ADSC)); //wait for conversion to complete
_delay_ms 10;
return (ADC);
}
int main(void)
{
init_ports_mcu(); //setup microcontroller i/o ports
ADC_init(); //initialize ADC
while (1)
{
switch(map(get_lightLevel(), 0, 1023, 0, 3)){ //read and map ADC values
case 0: //high light intensifies - LED is off
PORTB &= ~(1<<LED1);
PORTC &= ~(1<<PHOTO1);
break;
case 1: //middle light intensifies - LED is on
PORTB = (1<<LED1);
PORTC &= ~(1<<PHOTO1);
break;
case 2: //low light intensifies - LED is on
PORTB = (1<<LED1);
PORTC = (1<<PHOTO1);
break;
}
}
return (0);
}
Errors:
Recipe for target 'main.o' failed - Line 76
expected ';' before numeric constant - Line 44
expected ';' before numeric constant - Line 49
Warnings:
Statement with no effect [wunused values] - Line 44
Statement with no effect [wunused values] - Line 49
Larger integer implicitly turnicated to unsigned type [wover flow] - Line 69
Larger integer implicitly turnicated to unsigned type [wover flow] - Line 73
The formatting of your delays is incorrect. According to util/delay.h
(Convenience functions for busy-wait delay loops) here, two functions are available:
void _delay_ms (double __ms)
void _delay_us (double __us)
You should treat these as function calls, so you need to write:
_delay_ms(10);