The User taps the touch screen to Turn ON/OFF a desired remote appliance. This is detected by the Primary microcontroller (Atmega2560) which broadcasts a command through the secondary microcontroller (ATmega328P) via the 433 MHz transmitter module. Additionally a RTC (Real Time Clock) connected to the Microcontroller displays the current time and date in the touch screen. On the remote receiver end the wireless signal is received by the 433 MHz receiver module, and the microcontroller decides which electrical appliance to turn ON/OFF, and does so by the relays.
https://www.youtube.com/watch?v=XmtmE8nkgBc
The Atmega168 Bare-Bone Arduino Transmitter:
#include "VirtualWire.h"
char inData[50]; // Allocate some space for the string
char outData[50];
char nullData[50];
char inChar = -1; // Where to store the character read
byte index = 0; // Index into array; where to store the character
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup");
vw_set_tx_pin(9);
// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
}
void loop()
{
//const char *msg = "hello";
while (Serial.available() > 0) // Don't read unless
// there you know there is data
{
if (index < 49) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it
index++; // Increment where to write next
inData[index] = '\0'; // Null terminate the string
}
}
Serial.print("The input data is: ");
Serial.println(inData);
if (strcmp(inData, nullData) == 0) {
delay(1);
}
else
{
strcpy(outData, inData);
}
for (int i = 0; i < 49; i++) {
inData[i] = 0;
}
index = 0;
Serial.print("The output data is: ");
Serial.println(outData);
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)outData, strlen(outData));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false);
delay(200);
for (int i = 0; i < 49; i++) {
outData[i] = 0;
}
}
The Touch Screen Controller (Transmitter):
#include "UTFT.h"
#include "UTouch.h"
#include "DigitalToggle.h"
#include "Wire.h"
#include "RTClib.h"
RTC_DS1307 RTC;
// Declare which fonts we will be using
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];
UTFT myGLCD(ITDB32S, 38, 39, 40, 41);
UTouch myTouch(6, 5, 4, 3, 2);
int x, y;
/*************************
** Custom functions **
*************************/
void drawButtons()
{
// Draw the fan on/off button
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (10, 130, 150, 180);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10, 130, 150, 180);
myGLCD.print("Fan On/Off", 40, 147);
//Draw the Light On/Off button
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (160, 130, 300, 180);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (160, 130, 300, 180);
myGLCD.print("Light On/OFF", 190, 147);
myGLCD.setBackColor (0, 0, 0);
// //Draw Fan Speed Control
// myGLCD.setColor(0, 0, 255);
// myGLCD.fillRoundRect (10, 185, 70, 235);
// myGLCD.setColor(255, 255, 255);
// myGLCD.drawRoundRect (10, 185, 70, 235);
// myGLCD.print("+", 30,205 );
//
}
// Draw a red frame while a button is touched
void waitForIt(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 displayTime()
{
DateTime now = RTC.now();
myGLCD.setFont(BigFont);
myGLCD.printNumI(now.day(), 70, 30);
myGLCD.print("/", 102, 30);
myGLCD.printNumI(now.month(), 118, 30);
myGLCD.print("/", 150, 30);
myGLCD.printNumI(now.year(), 166, 30);
int current_hour = now.hour();
if (current_hour > 11) {
current_hour = current_hour - 12;
myGLCD.setFont(BigFont);
myGLCD.print("PM", 275, 90);
}
else {
myGLCD.setFont(BigFont);
myGLCD.print("AM", 275, 90);
}
myGLCD.setFont(SevenSegNumFont);
myGLCD.printNumI(current_hour, 20, 60);
myGLCD.setFont(BigFont);
myGLCD.print(":", 89, 77);
myGLCD.setFont(SevenSegNumFont);
myGLCD.printNumI(now.minute(), 110, 60);
myGLCD.setFont(BigFont);
myGLCD.print(":", 179, 77);
myGLCD.setFont(SevenSegNumFont);
myGLCD.printNumI(now.second(), 200, 60);
myGLCD.setFont(SmallFont);
}
/*************************
** Required functions **
*************************/
void setup()
{
Serial.begin(9600);
Serial2.begin(9600);
Wire.begin();
RTC.begin();
// Initial setup
myGLCD.InitLCD();
myGLCD.clrScr();
myTouch.InitTouch();
myTouch.setPrecision(PREC_MEDIUM);
myGLCD.setFont(SmallFont);
myGLCD.setBackColor(0, 0, 255);
drawButtons();
}
void loop()
{
while (true)
{
displayTime();
delay(5);
myGLCD.setFont(SmallFont);
if (myTouch.dataAvailable())
{
myTouch.read();
x = myTouch.getX();
y = myTouch.getY();
if ((y >= 130) && (y <= 180)) // button row
{
if ((x >= 10) && (x <= 150)) // Button: Fan ON/OFF
{
waitForIt(10, 130, 150, 180);
//enter action
Serial2.println("ToggleFan");
Serial.println("ToggleFan");
myGLCD.setFont(BigFont);
myGLCD.setColor(VGA_YELLOW);
myGLCD.print("Fan State Toggled ", 15, 210);
delay(500);
myGLCD.print(" ", 15, 210);
delay(500);
myGLCD.print("Fan State Toggled ", 15, 210);
delay(500);
myGLCD.print(" ", 15, 210);
}
if ((x >= 160) && (x <= 300)) // Button: Light ON/OFF
{
waitForIt(160, 130, 300, 180);
//enter action
Serial2.println("ToggleLight");
Serial.println("ToggleLight");
myGLCD.setFont(BigFont);
myGLCD.setColor(VGA_LIME);
myGLCD.print("Light State Toggled", 15, 210);
delay(500);
myGLCD.print(" ", 15, 210);
delay(500);
myGLCD.print("Light State Toggled", 15, 210);
delay(500);
myGLCD.print(" ", 15, 210);
}
}
}
}
}
The Arduino Mega 2560 Receiver:
#include "VirtualWire.h"
#include "DigitalToggle.h"
int led_pin = 13;
int fan_pin = 8;
int light_pin = 9;
int dc_fan = 12;
char inData[50]; // Allocate some space for the string
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup");
pinMode(led_pin, OUTPUT);
pinMode(fan_pin, OUTPUT);
pinMode(light_pin, OUTPUT);
pinMode(dc_fan, OUTPUT);
// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_set_rx_pin(7);
vw_rx_start(); // Start the receiver PLL running
}
void loop()
{
digitalWrite(dc_fan, HIGH);
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;
digitalWrite(13, true); // Flash a light to show received good message
// Message with a good checksum received, dump it.
//Serial.print("Got: ");
for (i = 0; i < buflen; i++)
{
// Serial.print(buf[i], HEX);
// Serial.print(" ");
inData[i] = buf[i];
}
//Serial.println("");
digitalWrite(13, false);
Serial.print("The received data is:");
Serial.print(inData);
Serial.print(" ");
Serial.print("The Length is: ");
Serial.print(buflen);
if (inData[6] == 'L') {
Serial.println("Toggle the light");
digitalToggle(light_pin);
}
if (inData[6] == 'F') {
Serial.println("Toggle the Fan");
digitalToggle(fan_pin);
}
}
}