code version 6
code version 6
//Version 6.0
//----------------------------------Library includes
-----------------------------------------------------------------------
#include <Adafruit_GFX.h> //https://github.com/adafruit/Adafruit-GFX-
Library/archive/master.zip
#include <Adafruit_ILI9341.h>
//https://github.com/adafruit/Adafruit_ILI9341/archive/master.zip
#include <URTouch.h> //http://www.rinkydinkelectronics.com/download.php?
f=URTouch.zip
#include <MCP4922.h> //https://github.com/helgenodland/MCP4922-Arduino-
SPI-Library/archive/master.zip
#include <SPI.h>
//-----------------------------------PIN
assignments------------------------------------------------------------------------
// Pins for TFT
#define TFT_DC 9 // Pin connection D/C display (data/command)
#define TFT_CS 10 // Pin of CS display output connection
#define TFT_RST 8 // Pin of output connection RESET (If connected
to power or button, then comment out this line, and uncomment the next one)
// #define TFT_RST -1 // If the display of the RESET is connected to
the power supply or the RESET button on the Arduino
#define TFT_MISO 12 // Pin of display output connection SDO(MISO)
#define TFT_MOSI 11 // Pin of display output connection SDI(MOSI)
#define TFT_CLK 13 // Pin of display output connection SCK
URTouch ts(t_SCK, t_CS, t_MOSI, t_MISO, t_IRQ); // Create an object of the sensor
module and inform the library of the pinout for working with it
//------------------------------------Global
variables------------------------------------------------------------------
float volts = 0.000; //set initial voltage
float voltsU = 0.000;
int x, y; //TFT screen cooridinates
char myInput[6]; //store number from keypad - was [5]
int n = 0; //index for myInput array
char key; //keypad character
int keyDelay = 500; //key bounce delay - was 1000 (change value as
required)
//------------------------------Programme Set-up
Routines--------------------------------------------------
void setup()
{
//------------------------------Main Programme
Loop--------------------------------------------------
void loop()
{
while (true)
{
if ((y >= 180) && (y <= 200)) //keypad top row coordinates
{
if ((x >= 110) && (x <= 180)) //Output ON button selected
{
digitalWrite(A2, HIGH); //switch output voltage ON
}
}
if (voltsU != volts) {
displayVoltage(); //print reference voltage on TFT
main display
}
//------------------------------Print Keypad to
Display--------------------------------------------------
void setVoltageKeypad()
{
tft.fillRect(70, 90, 180, 120, ILI9341_RED); //fill keypad area with RED
tft.fillRect(255, 180, 60, 30, ILI9341_RED); //Draw RED button for SET Voltage
on keypad
tft.drawRect(255, 180, 60, 30, ILI9341_WHITE); //Draw WHITE outline on SET
button on keypad
tft.setTextColor(ILI9341_YELLOW); // Determine the color of text
for display
tft.setTextSize(2); // Determine the font size for
display
tft.setCursor(270, 190); // Determine the coordinates for
printing SET on keypad
tft.print("SET"); // Print SET on keypad
Serial.println(" ");
Serial.print("X = "); Serial.print(x);
Serial.print(" Y = "); Serial.print(y);
}
}
//------------------------------Keypad Entry
Routine-----------------------------------------------------------
void keyPadEntry() //keypad entry routine
{
while (true)
{
readTouchScreen();
n = 0;
return;
}
}
}
}
} else if (volts > 4.000 && volts < 8.001) { //if voltage is greater than 4V
and less than 8.001V
volts = volts / 2; //divide set voltage by 2 for
input to DAC
digitalWrite(A4, HIGH); //set reference voltage to 4.094V
digitalWrite(A1, LOW); //set INA105 gain switch to x1
digitalWrite(A3, HIGH); //set INA105 gain switch to x2
SPI.begin(); //Initializes the SPI bus
DAC.Set((volts * 1000), 0); //write voltage to DAC
SPI.end(); //Disables the SPI bus
volts = volts * 2; //set TFT display reading to
input reading
} else if (volts > 2.000 && volts < 4.001) { //if voltage is greater than 2V
and less than 4.001V
digitalWrite(A4, HIGH); //set reference voltage to 4.094V
digitalWrite(A1, LOW); //set INA105 gain switch to x1
digitalWrite(A3, LOW); //set INA105 gain switch to x1
SPI.begin(); //Initializes the SPI bus
DAC.Set((volts * 1000), 0); //write voltage to DAC
SPI.end(); //Disables the SPI bus
} else if (volts > 1.000 && volts < 2.001) { //if voltage is greater than 1V
and less than 2.001V
digitalWrite(A4, LOW); //set reference voltage to 1.024V
digitalWrite(A1, LOW); //set INA105 gain switch to x1
digitalWrite(A3, HIGH); //set INA105 gain switch to x2
SPI.begin(); //Initializes the SPI bus
DAC.Set((volts * 2000), 0); //write voltage to DAC
SPI.end(); //Disables the SPI bus
myInput[n + 1] = '\0';
}
tft.setTextSize(3); // Determine the font size for display
tft.setCursor(70, 50); // Determine the coordinates of the upper-left
corner of the output area
tft.print(myInput); // Display the text
delay(keyDelay);
x = 0;
y = 0;
}
//---------------------------------------------------------------------------------
---------------------------