#include <LibPrintf.h>
//#include "megaAVR_TimerInterrupt.hpp" // 2560 tidak dicover
//#include <Wire.h>
//#include <TimeLib.h>
//#include "DS1307RTC.h"
#include <Servo.h>
#include "RTClib.h"
#define LED_L 13
#define LED_MERAH 10
#define LED_HIJAU 12
#define Pot1 A1
#define Pot2 A0
#define NTC A2
boolean toggle4 = LOW;
int iLoop = 0;
bool bBlinkLed = false;
int nPot1 = 0;
int nPot2 = 0;
int nCnt = 0;
const float BETA = 3950;
void setup() {
// put your setup code here, to run once:
pinAssignment();
timer4Init();
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
if (iLoop>=10) {
nCnt++;
led_blink(bBlinkLed);
bBlinkLed = !bBlinkLed;
iLoop = 0;
printf("[%4d] led beraksi Pot1: %d, Pot2: %d, suhu: %.2f ℃\r\n", nCnt, nPot1, nPot2, hitungSuhu());
}
delay(10);
iLoop++;
nPot1 = analogRead(Pot1);
nPot2 = analogRead(Pot2);
}
void led_blink(bool i) {
if (i) digitalWrite(LED_MERAH, HIGH);
else digitalWrite(LED_MERAH, LOW);
}
void pinAssignment() {
pinMode(LED_MERAH, OUTPUT);
pinMode(LED_L, OUTPUT);
pinMode(Pot1, INPUT);
pinMode(Pot2, INPUT);
}
float hitungSuhu() {
int analogValue = analogRead(NTC);
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
return celsius;
}
void timer4Init() {
cli();
//set timer4 interrupt at 1Hz
TCCR4A = 0;// set entire TCCR1A register to 0
TCCR4B = 0;// same for TCCR1B
TCNT4 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
OCR4A = 15624/1;// = (16*10^6) / (1*1024) - 1 (must be <65536)
TCCR4B |= (1 << WGM12); // turn on CTC mode
TCCR4B |= (1 << CS12) | (1 << CS10); // Set CS12 and CS10 bits for 1024 prescaler
TIMSK4 |= (1 << OCIE4A); // enable timer compare interrupt
sei();//allow interrupts
}
ISR(TIMER4_COMPA_vect){//timer1 interrupt 1Hz toggles pin 13 (LED)
//generates pulse wave of frequency 1Hz/2 = 0.5kHz (takes two cycles for full wave- toggle high then toggle low)
digitalWrite(LED_L,toggle4);
toggle4 = !toggle4;
}