Hello,
I have a little project at the university where I have to use the timers to generate a pwm signal and get it into a piezo buzzer when something else turns over a certain value by using the registers, the thing is that we have worked on some other microcontrollers so I don't really know the libraries for arduino uno with atmega328p and I would require some help on that. Also I have to use the registers instead of the function analogRead() that is reading from a temperature sensor.
Having the following code that works for my setup:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //Digital pins to which you connect the LCD
const int inPin = 0;
const int buzzer=9;// A0 is the sensor
void setup()
{
pinMode(buzzer,OUTPUT); // DDRA=0b00000001;
lcd.begin(16,2);
}
void loop()
{
int value = analogRead(inPin); // read the value from the sensor <= to be written on registers
lcd.setCursor(0,1);
float millivolts = (value / 1024.0) * 5;
float celsius = (millivolts-0.5)*100;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(celsius);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print((celsius * 9)/5 + 32); //turning the celsius into fahrehait
lcd.print("F");
delay(1000);
if(celsius>30.0){tone(buzzer,1000);delay(1000);noTone(buzzer);delay(1000);} //<= on registers
}