Hello everyone,
I want to build a tachometer for measuring the rotational speed of small-scale turbines that I will build. I bought an Arduino Mega 2560, a 5'' TFT Display with SSD1963 controller board and an IR sensor MH Sensor series.
Firstly, I have programmed the micro-controller without display and turbines that measuring the rotational speed with the IR sensor.
Now I am programming the display (the home screen is finished and the rpm appear correctly), however I have a problem since I began the touch screen part. I would like to create 4 buttons ( one for my 3 different turbines + 1 reset button). My objective is to be able to save the rpm max for each turbine and to be able to reset.
The most strange is that the program upload with no problem, I see the screen turn on 1s and turn off.
Someone can help me?
#include <avr/pgmspace.h> // To include the file.c
#include <UTFT.h> // Display informations
#include <TimerOne.h> // Timer one run the periodic interrupt function
#include <UTouch.h> // Touch screen
// Declare which fonts we will be using
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];
//extern uint8_t Calibri32x64GR[24324];
// Declare the bitmap
//extern unsigned short rsz_coerarduinocrop[13872];
// Creating objects
UTFT myGLCD(SSD1963_480272, 38, 39, 40, 41); //(Model, Pin for Register Select (RS), Pin for Write (WR), Pin for Chip Select (CS), Pin for Reset (RST), int SER)
UTouch myTouch (15, 10, 14, 9, 8); // UTouch myTouch ( Pin for Touch Clock, Pin for Touch Chip Select, Pin for Touch Data input, Pin for Touch Data output, Pin for Touch IRQ )
// Defining variables
int x, y;
char currentPage;
const int IRSensorPin = 2;
const int ledPin = 13;
int ledState = HIGH;
int inputState;
int lastInputState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 5;
long time;
long endTime;
long startTime;
int RPM = 0;
float lnTime = 0;
int rpm_max = 0;
// ---------------------------------------------------------------
void drawHomeScreen() {
myGLCD.fillScr(0, 167, 255);
//Startup text on display
myGLCD.setBackColor(120, 60, 180);
myGLCD.setColor(255, 255, 255);
myGLCD.setFont(BigFont);
myGLCD.print("RPM", RIGHT, 110);
myGLCD.setColor(255, 255, 255);
myGLCD.setFont(BigFont);
myGLCD.print("HIGHEST", LEFT, 230);
// Button - Turbine 1
myGLCD.setColor(16, 167, 103);
myGLCD.fillRoundRect (10, 290, 76, 330);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10, 290, 76, 330);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(16, 167, 103);
myGLCD.print("T1", 30, 310);
// Button - Turbine 2
myGLCD.setColor(16, 167, 103);
myGLCD.fillRoundRect (96, 290, 162, 330);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (96, 290, 162, 330);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(16, 167, 103);
myGLCD.print("T2", 116, 310);
// Button - Turbine 3
myGLCD.setColor(16, 167, 103);
myGLCD.fillRoundRect (182, 290, 250, 330);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (182, 290, 250, 330);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(16, 167, 103);
myGLCD.print("T3", 202, 310);
// Button - RESET
myGLCD.setColor(16, 103, 167);
myGLCD.fillRoundRect (40, 380, 218, 430);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (40, 380, 218, 430);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(16, 103, 167);
myGLCD.print("RESET", 90, 405);
}
// ----------------------------------------------------------------
void timerIsr()
{
// Print RPM every second, RPM based on timer
Serial.println("---------------");
time = millis() / 1000;
Serial.print(time);
Serial.print(" RPM: ");
Serial.println(RPM);
Serial.print(" rpm_max: ");
Serial.println(rpm_max);
myGLCD.setFont(BigFont);
myGLCD.printNumI(RPM, LEFT, 110, 3);
myGLCD.setFont(BigFont);
myGLCD.printNumI(rpm_max, RIGHT, 230, 3);
delay(500);
RPM = 0;
}
// ---------------------------------------------------------------
void calculateRPM() {
startTime = lastDebounceTime;
lnTime = startTime - endTime;
RPM = 60000 / (startTime - endTime);
endTime = startTime;
}
// ---------------------------------------------------------------
// Highlights the button when pressed
void drawFrame(int x1, int y1, int x2, int y2) {
myGLCD.setColor(255, 0, 0);
myGLCD.drawRoundRect (x1, y1, x2, y2);
while (myTouch.dataAvailable())
myTouch.read();
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (x1, y1, x2, y2);
}
// ---------------------------------------------------------------
void setup() {
// Setup the LCD
myGLCD.InitLCD(0);
myGLCD.clrScr();
myTouch.InitTouch();
myTouch.setPrecision(PREC_MEDIUM);
//Defining Pin Modes and set Serial port ON
pinMode(IRSensorPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);
Serial.begin(9600);
// Home screen
drawHomeScreen(); // Draws the Home Screen
currentPage='0';
//Initialize TimerOne
endTime = 0;
Timer1.initialize(1000000); // Set the timer to 60 rpm, 1,000,000 microseconds (1 second)
Timer1.attachInterrupt(timerIsr); // Attach the service routine here
}
// ---------------------------------------------------------------
void loop(void) {
//Home screen
if (currentPage == '0') {
if (myTouch.dataAvailable()) {
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
// If we press the T1 Button
if ((x>=10) && (x<=76) && (y>=290) && (y<=330)) {
drawFrame(10, 290, 76, 330); // Custom Function -Highlighs the buttons when it's pressed
currentPage = '1';
//myGLCD.clrScr();
//T1(); // It is called only once, because in the next iteration of the loop, this above if statement will be false so this funtion won't be called. This function will draw the graphics of T1.
}
// If we press the T2 Button
if ((x>=96) && (x<=162) && (y>=290) && (y<=330)) {
drawFrame(96, 290, 162, 330);
currentPage = '2';
//myGLCD.clrScr();
//T2();
}
// If we press the T3 Button
if ((x>=182) && (x<=250) && (y>=290) && (y<=330)) {
drawFrame(182, 290, 250, 330);
currentPage = '3';
//myGLCD.clrScr();
//T3();
}
// If we press the RESET Button
if ((x>=40) && (x<=218) && (y>=380) && (y<=430)) {
drawFrame(40, 380, 218, 430);
currentPage = '4';
//myGLCD.clrScr();
//RESET();
}
}
}
// draw the image to the screen
//myGLCD.drawBitmap(0, 0, 204, 68, rsz_coerarduinocrop);
// RPM counter
time = millis();
int currentSwitchState = digitalRead(IRSensorPin);
if (currentSwitchState != lastInputState) { // if the currentSwitch is HIGH we count the time
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) { // If the time the program starts less the elapsed time since the currentswitchState change > Debounce delay (5ms)
if (currentSwitchState != inputState) { // + if the currentSwitchState is different than the input state DO input state = currentSwitchState
inputState = currentSwitchState; // + if the input state is low DO led 13 on + calculate RPM + toggle LedState HIGH to LOW
if (inputState == LOW) {
digitalWrite(ledPin, LOW);
calculateRPM(); // Real RPM from sensor
ledState = !ledState;
}
else { // else the pin13 turn on
digitalWrite(ledPin, HIGH);
}
}
}
lastInputState = currentSwitchState;
// RPM max
if (RPM>rpm_max) {
rpm_max=RPM;
}
}
// ---------------------------------------------------------------