#include "TimerOne.h"
#include <LiquidCrystal_I2C.h>
/****************************************************************/
#define I2C_ADDR (0x27)
#define LCD_COLUMNS (20)
#define LCD_LINES (2)
#define ADC_MIN (0)
#define ADC_MAX (1023)
#define VELOCITY_MIN (0)
#define VELOCITY_MAX (255)
#define FIRST_LINES (0)
#define SECOND_LINES (1)
#define START_COLUMNS (0)
// Arduino D9 pin (PWM output)
#define PWM_PIN (10)
// Arduino Analog A0 pin
#define ADC_PIN (A0)
// PWM Period
#define PWM_PERIOD_US (1024)
#define SWITCH_ON (2) // Pin D18 SWITCH ON
#define SWITCH_DIR (8) // Pin D19 SWITCH OFF
#define BUTTON_START (digitalRead(SWITCH_ON) == HIGH)
#define BUTTON_DIR (digitalRead(SWITCH_DIR) == LOW)
#define Debounce (1000) // SW debounce
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
int potpin = A0; // analog pin used to connect the potentiometer
float val; // variable to read the value from the analog pin
String Switch_cmd_State = "";
String Switch_dir_State = "";
bool Switch_cmd = LOW;
bool Switch_dir = LOW;
//-------- Set flag even control---
unsigned long lastime = 0;
unsigned long lastime1 = 0;
/****************************************************************/
void setup()
{
Serial.begin(9600); // Start Serial communication
pinMode(PWM_PIN, OUTPUT);
pinMode(SWITCH_ON, INPUT);
pinMode(SWITCH_DIR, INPUT);
Timer1.initialize(PWM_PERIOD_US); // PWM period in usec
Timer1.pwm(PWM_PIN, 0, PWM_PERIOD_US); // PWM output (initially 0%)
Timer1.attachInterrupt([]()
{
// ISR function
// Update the PWM duty cycle
Timer1.setPwmDuty(PWM_PIN, analogRead(ADC_PIN));
});
// Init
lcd.init();
lcd.backlight();
// Print something
lcd.setCursor(START_COLUMNS, FIRST_LINES);
lcd.print("Hi Mechatronics");
lcd.setCursor(START_COLUMNS, SECOND_LINES);
lcd.print("Microcontrollers");
delay(5000);
lcd.clear();
}
void loop()
{
int temp_val = analogRead(ADC_PIN);
val = map(temp_val, ADC_MIN, ADC_MAX, VELOCITY_MIN, VELOCITY_MAX); // scale it to use it with the motor
Display_LCD(val, temp_val);
Read_Switches_State();
Serial_display();
delay(1000);
}
/****************************************************************/
void Read_Switches_State()
{
if(BUTTON_START)
{
long now1 = millis();
if((now1 - lastime) > Debounce) // Software debounce
{
lastime = now1;
Switch_cmd = !Switch_cmd;
}
}
if(BUTTON_DIR)
{
long now = millis();
if((now - lastime1) > Debounce) // Software debounce
{
lastime1 = now;
Switch_dir = !Switch_dir;
}
Switch_cmd_State = (Switch_cmd == HIGH) ? "On" : "Off";
Switch_dir_State = (Switch_dir == HIGH) ? "On" : "Off";
}
}
/****************************************************************/
void Display_LCD(float input_val, int temp_input_val)
{
lcd.setCursor(START_COLUMNS, FIRST_LINES);
lcd.print("Velocity (m/s):");
lcd.setCursor(START_COLUMNS + 1, SECOND_LINES);
lcd.print(input_val / 10.0); // ปรับค่าให้เหมาะสมตามที่ต้องการ
lcd.setCursor(START_COLUMNS + 7, SECOND_LINES);
lcd.print(temp_input_val);
lcd.setCursor(START_COLUMNS + 11, SECOND_LINES);
lcd.print("(ADC)");
}
/****************************************************************/
void Serial_display()
{
Serial.print("Command Run: ");
Serial.println(Switch_cmd_State);
Serial.print("Command Dir: ");
Serial.println(Switch_dir_State);
}