#define ENCODER_CLK 18
#define ENCODER_DT 19
#define ENCODER_SW 35
#include <RTClib.h>
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
#include <SdFat.h>
//#include <SD.h>
#include <SPI.h>
hd44780_I2Cexp lcd; //created the object 'lcd'
RTC_DS1307 rtc;
const int lcdCols = 20;
const int lcdRows = 4;
int counter = 0;
float time = 0;
int potSpeedPin = A0; // Set analog pin for motor speed input
int currentStateRPM = 0;
int lastStateRPM = 0;
unsigned long lastButtonPress = 6;
// Motor A connections
int enA = 8; // Enable
int in1 = 6; // Direction control 1
int in2 = 7; // Direction control 2
int lightCount = 0; // thrust counter
int startSwitch = 39; // Switch to start the motor
int statusLED = 3; // for top mounted LED
int lastClk = HIGH;
// setting of flags for logic states
#define SPI_SPEED SD_SCK_MHZ(4)
#define CS_PIN 53
SdFat sd;
void setup() {
Serial.begin(115200);
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
int statusLCD;
statusLCD = lcd.begin(lcdCols, lcdRows);
if(statusLCD){
hd44780::fatalError(statusLCD);
}
lcd.backlight(); //Switches on Backlight
lcd.clear(); //Clears LCD and Chip Buffer
// Set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
// Turn off motors - Initial state
digitalWrite(in1, LOW); //set motor to OFF
digitalWrite(in2, LOW); //set motor to OFF
pinMode(startSwitch, INPUT);
TCCR4B = TCCR4B & B11111000 | B00000010; // for PWM frequency of 3921.16 Hz on pin 6,7,8
pinMode(statusLED, OUTPUT);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (!sd.begin(CS_PIN, SPI_SPEED)) {
if (sd.card()->errorCode()) {
Serial.println("SD initialization failed.");
} else if (sd.vol()->fatType() == 0) {
Serial.println("Can't find a valid FAT16/FAT32 partition.");
} else {
Serial.println("Can't determine error type");
}
return;
}
Serial.println("Files on card:");
Serial.println(" Size Name");
sd.ls(LS_R | LS_SIZE);
lcd.setCursor(0, 0); //Sets the cursour position, coloum followed by row, i.e. row 4 coloum 0
lcd.print("FileName:"); //Prints to LCD
lcd.setCursor(0, 1); //Sets the cursour position, coloum followed by row, i.e. row 4 coloum 0
lcd.print("RPM:"); //Prints to LCD
lcd.setCursor(0, 2); //Sets the cursour position, coloum followed by row, i.e. row 4 coloum 0
lcd.print("Time:"); //Prints to LCD
lcd.setCursor(10,2);
lcd.print("min");
lcd.setCursor(0, 3); //Sets the cursour position, coloum followed by row, i.e. row 4 coloum 0
lcd.print("Count:"); //Prints to LCD
lcd.setCursor(11,3);
lcd.print("Angle:");
}
void loop() {
int newClk = digitalRead(ENCODER_CLK);
if (newClk != lastClk) {
// There was a change on the CLK pin
lastClk = newClk;
int dtValue = digitalRead(ENCODER_DT);
if (newClk == LOW && dtValue == HIGH) {
//Serial.println("Rotated clockwise ⏩");
counter++;
}
if (newClk == LOW && dtValue == LOW) {
//Serial.println("Rotated counterclockwise ⏪");
counter--;
}
}
if (counter <= 0){
counter = 0;
}
// Serial.println(counter);
if (time != (counter/2.0)){
lcd.setCursor(14,1);
lcd.print(" ");
lcd.setCursor(14,2);
lcd.print(" ");
Serial.println("in here");
}
time = counter/2.0;
lcd.setCursor(5,2);
lcd.print(time);
// Read the button state
int btnState = digitalRead(ENCODER_SW);
//If we detect LOW signal, button is pressed
if (btnState == LOW) {
//if 50ms have passed since last LOW pulse, it means that the
//button has been pressed, released and pressed again
if (millis() - lastButtonPress > 50) {
Serial.println("Button pressed!");
lcd.setCursor(14,2);
lcd.print("SET");
double time = counter;
Serial.println(time);
lcd.setCursor(14,1);
lcd.print("SET");
}
// Remember last button press event
lastButtonPress = millis();
}
int motorSpeedPotVal = analogRead(potSpeedPin);
int motorSpeedRPM = map(motorSpeedPotVal, 0 , 1023 ,24 , 45);
currentStateRPM = motorSpeedRPM;
/*
Serial.print("current state RPM: ");
Serial.println(currentStateRPM);
Serial.print("last state RPM: ");
Serial.println(lastStateRPM);
*/
if (currentStateRPM != lastStateRPM){
int setRPM = currentStateRPM;
lcd.setCursor(4,1);
lcd.print(setRPM);
Serial.println(setRPM);
lcd.setCursor(14,1);
lcd.print(" ");
lcd.setCursor(14,2);
lcd.print(" ");
int motorSpeedPWM = map(motorSpeedPotVal, 0, 1023, 130, 255);
lastStateRPM = currentStateRPM;
}
DateTime now = rtc.now();
int year = (now.year() - 2000);
int month = (now.month());
int day = (now.day());
int hour = (now.hour());
int minute = (now.minute());
String fileName = String(year);
if (month < 10){
fileName = fileName + "0" + String(month);
}
else{
fileName = fileName + String(month);
}
if (day < 10){
fileName = fileName + "0" + String(day);
}
else{
fileName = fileName + String(day);
}
if (hour < 10){
fileName = fileName + "0" + String(hour);
}
else{
fileName = fileName + String(hour);
}
if (minute < 10){
fileName = fileName + "0" + String(minute);
}
else{
fileName = fileName + String(minute);
}
lcd.setCursor(9,0);
lcd.print(fileName);
// Logic flow as follows
/*
if set flag is true and switch is pressed
DateTime now = rtc.now();
int year = (now.year() - 2000);
int month = (now.month());
int day = (now.day());
int hour = (now.hour());
int minute = (now.minute());
String fileName = String(year);
if (month < 10){
fileName = fileName + "0" + String(month);
}
else{
fileName = fileName + String(month);
}
if (day < 10){
fileName = fileName + "0" + String(day);
}
else{
fileName = fileName + String(day);
}
if (hour < 10){
fileName = fileName + "0" + String(hour);
}
else{
fileName = fileName + String(hour);
}
if (minute < 10){
fileName = fileName + "0" + String(minute);
}
else{
fileName = fileName + String(minute);
}
lcd.setCursor(9,0);
lcd.print(fileName);
print the file name to the screen
initiate the SD file
print first line of string
Time, RPM, Force, tilt 1, tilt 2, sensor counter
Set a flag to say "operation has begun"
return and skip the rest
if the "operation has begun" flag has been set
calculate time needed
set time variable
set motor to on
set initial time
return and skip the rest
if millis - previous time < time variable && if operation has begun
print sensor value to SD
print tilt sensor to SD
print force sensor to SD
print time to SD
print RPM to SD
return and skip the rest
if millis - previous time >= time variable && if operation has begun
stop motor
save SD file
close SD file
reset all flags
reset all counters
reset saved variables
return and skip the rest
if reset button pressed
reset all flags
reset all counters
reset saved variables
return and skip the rest
analogWrite(enA, setRPM);
igitalWrite(statusLED, HIGH);
*/
}