Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Keypad.h>
- #include <SPI.h>
- #include <DS3231.h>
- #include <SD.h>
- #include <SPFD5408_Adafruit_GFX.h> // Core graphics library
- #include <SPFD5408_Adafruit_TFTLCD.h> // Hardware-specific library
- #include <SPFD5408_TouchScreen.h>
- #define LCD_CS A3 // Chip Select goes to Analog 3
- #define LCD_CD A2 // Command/Data goes to Analog 2
- #define LCD_WR A1 // LCD Write goes to Analog 1
- #define LCD_RD A0 // LCD Read goes to Analog 0
- #define LCD_RESET A4
- Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
- #define BLACK 0x0000
- #define BLUE 0x001F
- #define RED 0xF800
- #define GREEN 0x07E0
- #define CYAN 0x07FF
- #define MAGENTA 0xF81F
- #define YELLOW 0xFFE0
- #define WHITE 0xFFFF
- /* //parte amp
- const int analogIn = A8;
- int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module
- int RawValue= 0;
- int ACSoffset = 2500;
- double Voltage = 0;
- double Amps = 0;
- */
- DS3231 rtc(SDA, SCL);
- Time t;
- #define PinoFalante 40
- void readV() {
- // initialize serial communication at 9600 bits per second:
- Serial.begin(9600);
- }
- // the loop routine runs over and over again forever:
- void loopp() {
- // read the input on analog pin 0:
- int sensorValue = analogRead(A10);
- // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
- float voltage = sensorValue * (5.0 / 1023.0);
- // print out the value you read:
- tft.setCursor(0, 0);
- tft.println("tensao: ");
- tft.println(voltage);
- }
- int porta_rele1 = 35;
- int porta_rele2 = 34;
- const byte ROWS = 4; //quatro linhas
- const byte COLS = 4; //quatro colunas
- char keys[ROWS][COLS] = {
- {'1','2','3','A'},
- {'4','5','6','B'},
- {'7','8','9','C'},
- {'*','0','#','D'}
- };
- byte rowPins[ROWS] = {22,24,26,28}; //connect to the row pinouts of the kpd
- byte colPins[COLS] = {23,25,27,29}; //connect to the column pinouts of the kpd
- Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
- int i;
- int hora=0;
- int horat=0;
- int horaf=0;
- int minuto=0;
- int minutot=0;
- int minutof=0;
- int segundo=0;
- char key = keypad.getKey();
- int finaliza=0;
- void setup(){
- Serial.begin(9600);
- progmemPrintln(PSTR("TFT LCD test"));
- tft.reset();
- tft.begin(0x9328); // SDFP5408
- tft.setRotation(1);
- rtc.begin();
- //Define pinos para o rele como saida
- pinMode(porta_rele1, OUTPUT);
- pinMode(porta_rele2, OUTPUT);
- digitalWrite(porta_rele1, HIGH); //Desliga rele 1
- digitalWrite(porta_rele2, HIGH); //Desliga rele 2
- while (finaliza==0){
- tft.setCursor(0, 0);
- tft.fillScreen(BLACK);
- loopp();
- tft.setTextSize(2);
- tft.setTextColor(WHITE);
- tft.print("Pressione 0 para ver fim e");
- tft.println("inicio do horario de pico");
- tft.println("");
- tft.print("Pressione 1 para definir o");
- tft.println("inicio do horario de pico");
- tft.println("");
- tft.print("Pressione 2 para definir o");
- tft.println("fim do horario de pico");
- tft.println("");
- tft.println("Pressione # depois de");
- tft.println("configurado para continuar");
- switch(char key = keypad.waitForKey()){
- case '0':
- tone(40, 1500, 30);
- tft.fillScreen(BLACK);
- tft.setCursor(0, 0);
- tft.println("INICIO HORARIO DE PICO: ");
- tft.print(hora);
- tft.print(":");
- tft.println(minuto);
- tft.println("FIM HORARIO DE PICO: ");
- tft.print(horaf);
- tft.print(":");
- tft.println(minutof);
- keypad.waitForKey();
- break;
- case '1':
- tone(40, 1500, 30);
- horainicio(hora);
- minutoinicio(minuto);
- break;
- case '2':
- tone(40, 1500, 30);
- horafim(horaf);
- minutofim(minutof);
- break;
- case '#':
- // tone(40, 1500, 30);
- loopp();
- finaliza = 1;
- break;
- }
- }
- tft.println("Apertou '#', horarios de");
- tft.println("inicio e fim definidos");
- delay(5000);
- }
- void loop(){
- t = rtc.getTime();
- tft.setCursor(0, 0);
- tft.fillScreen(BLACK);
- tft.setTextSize(3);
- tft.setTextColor(WHITE);
- //mostra hora
- tft.println("");
- tft.print("HORA : ");
- tft.print(t.hour, DEC);
- tft.print(":");
- tft.print(t.min, DEC);
- tft.print(":");
- tft.println(t.sec, DEC);
- horat =t.hour;
- minutot = t.min;
- int horainicio = hora;
- int minutoinicio= minuto;
- int horafim = horaf;
- int minutofim = minutof;
- //lecorrente //parte amp
- if ( (horat < horainicio) || ((horat == horainicio) &&(minutot < minutoinicio)) || ((horat == horafim) &&(minutot > minutofim)) || (horat > horafim) ){
- //carrega bateria
- tft.println("carrega bat.");
- digitalWrite(porta_rele1, HIGH); //Desliga rele 1
- digitalWrite(porta_rele2, HIGH); //Desliga rele 2
- }
- else {
- tft.println("usa bat.");
- digitalWrite(porta_rele1, LOW); //liga rele 1
- digitalWrite(porta_rele2, LOW); //liga rele 2
- }
- delay(1000);
- }
- int horainicio(int &hora){
- tft.setCursor(0, 0);
- tft.fillScreen(BLACK);
- tft.setTextSize(3);
- tft.setTextColor(WHITE);
- int i;
- char hr[2];
- tft.println("defina a hora");
- tft.print("de inicio: ");
- for (i=0; i< 2; i++){
- char key = keypad.waitForKey();
- if((key != '*')&&(key!='#')&&(key!='A')&&(key!='B')&&(key!='C')&&(key!='D')){
- hr[i]=key;
- tft.print(hr[i]);
- }
- else{
- i--;
- }
- tone(40, 1500, 30);
- }
- // Serial.print("HORA CHAR: ");
- // Serial.println(hr);
- hora = atoi(hr);
- if (hora>23){
- tone(40, 666, 2000);
- tft.println();
- // Serial.print("HORA INT: ");
- // Serial.println(hora);
- tft.println("hora invalida,");
- tft.println("digite novamente.");
- delay(3000);
- horainicio(hora);
- }
- // Serial.print("HORA INT: ");
- // Serial.println(hora);
- Serial.println();
- return hora;
- }
- int minutoinicio(int &minuto){
- tft.setCursor(0, 0);
- tft.fillScreen(BLACK);
- tft.setTextSize(3);
- tft.setTextColor(WHITE);
- int i;
- char mi[2];
- tft.println("defina o minuto");
- tft.print("de inicio :");
- for (i=0; i< 2; i++){
- char key = keypad.waitForKey();
- if((key != '*')&&(key!='#')&&(key!='A')&&(key!='B')&&(key!='C')&&(key!='D')){
- mi[i]=key;
- tft.print(mi[i]);
- }
- else{
- i--;
- }
- tone(40, 1500, 30);
- }
- minuto = atoi(mi);
- if (minuto >59){
- tone(40, 666, 2000);
- tft.println();
- tft.println("minutos invalidos,");
- tft.println("digite novamente.");
- delay(3000);
- minutoinicio(minuto);
- }
- Serial.println();
- tft.fillScreen(BLACK);
- return minuto;
- }
- int horafim(int &horaf){
- tft.setCursor(0, 0);
- tft.fillScreen(BLACK);
- tft.setTextSize(3);
- tft.setTextColor(WHITE);
- int i;
- char hr[2];
- for (i=0; i< 2; i++){
- hr[i]= "";
- }
- tft.println("defina a hora de");
- tft.println("fim de pico: ");
- for (i=0; i< 2; i++){
- char key = keypad.waitForKey();
- if((key != '*')&&(key!='#')&&(key!='A')&&(key!='B')&&(key!='C')&&(key!='D')){
- hr[i]=key;
- tft.print(hr[i]);
- }
- else{
- i--;
- }
- tone(40, 1500, 30);
- }
- horaf = atoi(hr);
- if (horaf >23){
- tone(40, 666, 2000);
- tft.println();
- tft.println("hora invalida,");
- tft.println("digite novamente.");
- delay(3000);
- horafim(horaf);
- }
- tft.println();
- tft.setCursor(0, 0);
- tft.fillScreen(BLACK);
- return horaf;
- }
- int minutofim(int &minutof){
- tft.setCursor(0, 0);
- tft.fillScreen(BLACK);
- tft.setTextSize(3);
- tft.setTextColor(WHITE);
- int i;
- char mi[2];
- tft.println("defina o minuto");
- tft.println("de fim de pico: ");
- for (i=0; i< 2; i++){
- char key = keypad.waitForKey();
- if((key != '*')&&(key!='#')&&(key!='A')&&(key!='B')&&(key!='C')&&(key!='D')){
- mi[i]=key;
- tft.print(mi[i]);
- }
- else{
- i--;
- }
- tone(40, 1500, 30);
- }
- minutof = atoi(mi);
- if (minutof >59){
- tone(40, 666, 2000);
- tft.println();
- tft.println("minutos invalidos,");
- tft.println("digite novamente.");
- delay(3000);
- minutofim(minutof);
- }
- tft.setCursor(0, 0);
- tft.fillScreen(BLACK);
- return minutof;
- }
- /* //parte amp
- void lecorrente(){
- RawValue = analogRead(analogIn);
- Voltage = (RawValue / 1024.0) * 5000; // Gets you mV
- Amps = ((Voltage - ACSoffset) / mVperAmp);
- tft.print("Raw Value = " ); // shows pre-scaled value
- tft.println(RawValue);
- tft.print("mV = "); // shows the voltage measured
- tft.println(Voltage,3); // the '3' after voltage allows you to display 3 digits after decimal point
- tft.print("Amps = "); // shows the voltage measured
- tft.println(Amps,3); // the '3' after voltage allows you to display 3 digits after decimal point
- }
- */
- #define BUFFPIXEL 50
- void bmpDraw(char *filename, int x, int y) {
- File bmpFile;
- int bmpWidth, bmpHeight; // W+H in pixels
- uint8_t bmpDepth; // Bit depth (currently must be 24)
- uint32_t bmpImageoffset; // Start of image data in file
- uint32_t rowSize; // Not always = bmpWidth; may have padding
- uint8_t sdbuffer[3*BUFFPIXEL]; // pixel in buffer (R+G+B per pixel)
- uint16_t lcdbuffer[BUFFPIXEL]; // pixel out buffer (16-bit per pixel)
- uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
- boolean goodBmp = false; // Set to true on valid header parse
- boolean flip = true; // BMP is stored bottom-to-top
- int w, h, row, col;
- uint8_t r, g, b;
- uint32_t pos = 0, startTime = millis();
- uint8_t lcdidx = 0;
- boolean first = true;
- if((x >= tft.width()) || (y >= tft.height())) return;
- Serial.println();
- progmemPrint(PSTR("Loading image '"));
- Serial.print(filename);
- Serial.println('\'');
- // Open requested file on SD card
- if ((bmpFile = SD.open(filename)) == NULL) {
- progmemPrintln(PSTR("File not found"));
- tft.setCursor(0, 0);
- tft.setTextColor(GREEN);
- tft.setTextSize(3);
- tft.println("Nao");
- tft.println("Encontrado");
- return;
- }
- // Parse BMP header
- if(read16(bmpFile) == 0x4D42) { // BMP signature
- progmemPrint(PSTR("File size: ")); Serial.println(read32(bmpFile));
- (void)read32(bmpFile); // Read & ignore creator bytes
- bmpImageoffset = read32(bmpFile); // Start of image data
- progmemPrint(PSTR("Image Offset: ")); Serial.println(bmpImageoffset, DEC);
- // Read DIB header
- progmemPrint(PSTR("Header size: ")); Serial.println(read32(bmpFile));
- bmpWidth = read32(bmpFile);
- bmpHeight = read32(bmpFile);
- if(read16(bmpFile) == 1) { // # planes -- must be '1'
- bmpDepth = read16(bmpFile); // bits per pixel
- progmemPrint(PSTR("Bit Depth: ")); Serial.println(bmpDepth);
- if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
- goodBmp = true; // Supported BMP format -- proceed!
- progmemPrint(PSTR("Image size: "));
- Serial.print(bmpWidth);
- Serial.print('x');
- Serial.println(bmpHeight);
- // BMP rows are padded (if needed) to 4-byte boundary
- rowSize = (bmpWidth * 3 + 3) & ~3;
- // If bmpHeight is negative, image is in top-down order.
- // This is not canon but has been observed in the wild.
- if(bmpHeight < 0) {
- bmpHeight = -bmpHeight;
- flip = false;
- }
- // Crop area to be loaded
- w = bmpWidth;
- h = bmpHeight;
- if((x+w-1) >= tft.width()) w = tft.width() - x;
- if((y+h-1) >= tft.height()) h = tft.height() - y;
- // Set TFT address window to clipped image bounds
- tft.setAddrWindow(x, y, x+w-1, y+h-1);
- for (row=0; row<h; row++) { // For each scanline...
- // Seek to start of scan line. It might seem labor-
- // intensive to be doing this on every line, but this
- // method covers a lot of gritty details like cropping
- // and scanline padding. Also, the seek only takes
- // place if the file position actually needs to change
- // (avoids a lot of cluster math in SD library).
- if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
- pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
- else // Bitmap is stored top-to-bottom
- pos = bmpImageoffset + row * rowSize;
- if(bmpFile.position() != pos) { // Need seek?
- bmpFile.seek(pos);
- buffidx = sizeof(sdbuffer); // Force buffer reload
- }
- for (col=0; col<w; col++) { // For each column...
- // Time to read more pixel data?
- if (buffidx >= sizeof(sdbuffer)) { // Indeed
- // Push LCD buffer to the display first
- if(lcdidx > 0) {
- tft.pushColors(lcdbuffer, lcdidx, first);
- lcdidx = 0;
- first = false;
- }
- bmpFile.read(sdbuffer, sizeof(sdbuffer));
- buffidx = 0; // Set index to beginning
- }
- // Convert pixel from BMP to TFT format
- b = sdbuffer[buffidx++];
- g = sdbuffer[buffidx++];
- r = sdbuffer[buffidx++];
- lcdbuffer[lcdidx++] = tft.color565(r,g/2,b);
- } // end pixel
- } // end scanline
- // Write any remaining data to LCD
- if(lcdidx > 0) {
- tft.pushColors(lcdbuffer, lcdidx, first);
- }
- progmemPrint(PSTR("Loaded in "));
- Serial.print(millis() - startTime);
- Serial.println(" ms");
- } // end goodBmp
- }
- }
- bmpFile.close();
- if(!goodBmp) progmemPrintln(PSTR("BMP format not recognized."));
- }
- // These read 16- and 32-bit types from the SD card file.
- // BMP data is stored little-endian, Arduino is little-endian too.
- // May need to reverse subscript order if porting elsewhere.
- uint16_t read16(File f) {
- uint16_t result;
- ((uint8_t *)&result)[0] = f.read(); // LSB
- ((uint8_t *)&result)[1] = f.read(); // MSB
- return result;
- }
- uint32_t read32(File f) {
- uint32_t result;
- ((uint8_t *)&result)[0] = f.read(); // LSB
- ((uint8_t *)&result)[1] = f.read();
- ((uint8_t *)&result)[2] = f.read();
- ((uint8_t *)&result)[3] = f.read(); // MSB
- return result;
- }
- // Copy string from flash to serial port
- // Source string MUST be inside a PSTR() declaration!
- void progmemPrint(const char *str) {
- char c;
- while(c = pgm_read_byte(str++)) Serial.print(c);
- }
- // Same as above, with trailing newline
- void progmemPrintln(const char *str) {
- progmemPrint(str);
- Serial.println();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement