Advertisement
matheus_king

Untitled

Nov 17th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.73 KB | None | 0 0
  1. #include <Keypad.h>
  2. #include <SPI.h>
  3. #include <DS3231.h>
  4. #include <SD.h>
  5. #include <SPFD5408_Adafruit_GFX.h>    // Core graphics library
  6. #include <SPFD5408_Adafruit_TFTLCD.h> // Hardware-specific library
  7. #include <SPFD5408_TouchScreen.h>
  8.  
  9. #define LCD_CS A3 // Chip Select goes to Analog 3
  10. #define LCD_CD A2 // Command/Data goes to Analog 2
  11. #define LCD_WR A1 // LCD Write goes to Analog 1
  12. #define LCD_RD A0 // LCD Read goes to Analog 0
  13.  
  14. #define LCD_RESET A4
  15.  
  16.  
  17.  
  18. Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
  19.  
  20.  
  21. #define  BLACK   0x0000
  22. #define BLUE    0x001F
  23. #define RED     0xF800
  24. #define GREEN   0x07E0
  25. #define CYAN    0x07FF
  26. #define MAGENTA 0xF81F
  27. #define YELLOW  0xFFE0
  28. #define WHITE   0xFFFF
  29.  
  30.  
  31.  
  32.  
  33. /* //parte amp
  34.  
  35. const int analogIn = A8;
  36. int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module
  37. int RawValue= 0;
  38. int ACSoffset = 2500;
  39. double Voltage = 0;
  40. double Amps = 0;
  41.  
  42.  
  43. */
  44.  
  45.  
  46.  
  47.  
  48. DS3231  rtc(SDA, SCL);
  49. Time  t;
  50. #define PinoFalante 40
  51.  
  52.  
  53.  
  54.  
  55. void readV() {
  56.   // initialize serial communication at 9600 bits per second:
  57.   Serial.begin(9600);
  58. }
  59.  
  60. // the loop routine runs over and over again forever:
  61. void loopp() {
  62.   // read the input on analog pin 0:
  63.   int sensorValue = analogRead(A10);
  64.   // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  65.   float voltage = sensorValue * (5.0 / 1023.0);
  66.   // print out the value you read:
  67.   tft.setCursor(0, 0);
  68.   tft.println("tensao: ");
  69.   tft.println(voltage);
  70.  
  71. }
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78. int porta_rele1 = 35;
  79. int porta_rele2 = 34;
  80.  
  81. const byte ROWS = 4; //quatro linhas
  82. const byte COLS = 4; //quatro colunas
  83. char keys[ROWS][COLS] = {
  84.   {'1','2','3','A'},
  85.   {'4','5','6','B'},
  86.   {'7','8','9','C'},
  87.   {'*','0','#','D'}
  88. };
  89. byte rowPins[ROWS] = {22,24,26,28}; //connect to the row pinouts of the kpd
  90. byte colPins[COLS] = {23,25,27,29}; //connect to the column pinouts of the kpd
  91.  
  92.  
  93. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  int i;
  118. int hora=0;
  119. int horat=0;
  120. int horaf=0;
  121. int minuto=0;
  122. int minutot=0;
  123. int minutof=0;
  124. int segundo=0;
  125.  char key = keypad.getKey();
  126.   int finaliza=0;
  127.  
  128. void setup(){
  129.  
  130.   Serial.begin(9600);
  131.  
  132.  progmemPrintln(PSTR("TFT LCD test"));
  133.  
  134.    tft.reset();
  135.  
  136.      
  137.     tft.begin(0x9328); // SDFP5408
  138.  
  139.   tft.setRotation(1);
  140.    rtc.begin();
  141.    
  142.     //Define pinos para o rele como saida
  143.   pinMode(porta_rele1, OUTPUT);
  144.   pinMode(porta_rele2, OUTPUT);
  145.   digitalWrite(porta_rele1, HIGH);  //Desliga rele 1
  146.   digitalWrite(porta_rele2, HIGH); //Desliga rele 2
  147.  
  148.  
  149.  
  150.   while (finaliza==0){
  151.       tft.setCursor(0, 0);
  152.        tft.fillScreen(BLACK);
  153.        loopp();
  154.  tft.setTextSize(2);
  155.   tft.setTextColor(WHITE);
  156.    tft.print("Pressione 0 para ver fim e");
  157.      tft.println("inicio do horario de pico");
  158.       tft.println("");
  159.   tft.print("Pressione 1 para definir o");
  160.    tft.println("inicio do horario de pico");
  161.     tft.println("");
  162.   tft.print("Pressione 2 para definir o");
  163.    tft.println("fim do horario de pico");
  164.     tft.println("");
  165.     tft.println("Pressione # depois de");
  166.     tft.println("configurado para continuar");
  167.    switch(char key = keypad.waitForKey()){
  168.     case '0':
  169.       tone(40, 1500, 30);
  170.         tft.fillScreen(BLACK);
  171.      tft.setCursor(0, 0);
  172.       tft.println("INICIO HORARIO DE PICO: ");
  173.   tft.print(hora);
  174.    tft.print(":");
  175.     tft.println(minuto);
  176.    
  177.  
  178. tft.println("FIM HORARIO DE PICO: ");
  179. tft.print(horaf);
  180.  tft.print(":");
  181.   tft.println(minutof);
  182.   keypad.waitForKey();
  183.     break;
  184.  
  185.    
  186.     case '1':
  187.       tone(40, 1500, 30);
  188.     horainicio(hora);
  189.     minutoinicio(minuto);
  190.    
  191.    
  192.     break;
  193.  
  194.  
  195.  
  196.     case '2':
  197.       tone(40, 1500, 30);
  198.      horafim(horaf);
  199.      minutofim(minutof);
  200.      break;
  201.  
  202.  
  203.    
  204.  
  205.  
  206.  case '#':
  207.  //     tone(40, 1500, 30);
  208.      loopp();
  209.      finaliza = 1;
  210.      break;
  211.  
  212.      
  213.    }
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223. }
  224. tft.println("Apertou '#', horarios de");
  225. tft.println("inicio e fim definidos");
  226. delay(5000);
  227.  
  228.    
  229.   }
  230.  
  231.  
  232. void loop(){
  233.    
  234. t = rtc.getTime();
  235.  
  236.  tft.setCursor(0, 0);
  237.  tft.fillScreen(BLACK);
  238.  tft.setTextSize(3);
  239.   tft.setTextColor(WHITE);
  240.  
  241.  
  242.  
  243.  
  244. //mostra hora
  245. tft.println("");
  246. tft.print("HORA : ");
  247.   tft.print(t.hour, DEC);
  248.   tft.print(":");
  249.  tft.print(t.min, DEC);
  250.  tft.print(":");
  251.  tft.println(t.sec, DEC);
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258. horat =t.hour;
  259. minutot = t.min;
  260. int horainicio = hora;
  261. int minutoinicio= minuto;
  262. int horafim = horaf;
  263. int minutofim = minutof;
  264.  
  265. //lecorrente //parte amp
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273. if ( (horat < horainicio) || ((horat == horainicio) &&(minutot < minutoinicio)) || ((horat == horafim) &&(minutot > minutofim)) || (horat > horafim) ){
  274.   //carrega bateria
  275.   tft.println("carrega bat.");
  276.    digitalWrite(porta_rele1, HIGH);  //Desliga rele 1
  277.   digitalWrite(porta_rele2, HIGH); //Desliga rele 2
  278.  
  279.   }
  280.   else {
  281.     tft.println("usa bat.");
  282.      digitalWrite(porta_rele1, LOW);  //liga rele 1
  283.   digitalWrite(porta_rele2, LOW); //liga rele 2
  284.  
  285.     }
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.   delay(1000);
  310.  
  311.  
  312. }
  313.  
  314. int horainicio(int &hora){
  315.      tft.setCursor(0, 0);
  316.  tft.fillScreen(BLACK);
  317.  tft.setTextSize(3);
  318.   tft.setTextColor(WHITE);
  319.   int i;
  320.   char hr[2];
  321.   tft.println("defina a hora");
  322.   tft.print("de inicio: ");
  323.    for (i=0; i< 2; i++){
  324.   char key = keypad.waitForKey();
  325.  
  326.    if((key != '*')&&(key!='#')&&(key!='A')&&(key!='B')&&(key!='C')&&(key!='D')){
  327.      
  328.    hr[i]=key;
  329.    tft.print(hr[i]);
  330.  
  331.    
  332.    }
  333.    else{
  334.    
  335.    i--;
  336.     }
  337.    tone(40, 1500, 30);
  338.      }
  339.     //  Serial.print("HORA CHAR: ");
  340.    // Serial.println(hr);
  341. hora = atoi(hr);
  342. if (hora>23){
  343.     tone(40, 666, 2000);
  344.    tft.println();
  345.    //  Serial.print("HORA INT: ");
  346.    //   Serial.println(hora);
  347.    tft.println("hora invalida,");
  348.     tft.println("digite novamente.");
  349.    delay(3000);
  350.    horainicio(hora);
  351.   }
  352.   // Serial.print("HORA INT: ");
  353.     //  Serial.println(hora);
  354.     Serial.println();
  355.  return hora;
  356.  
  357.  
  358.  
  359.   }
  360. int minutoinicio(int &minuto){
  361.      tft.setCursor(0, 0);
  362.  tft.fillScreen(BLACK);
  363.  tft.setTextSize(3);
  364.   tft.setTextColor(WHITE);
  365.   int i;
  366.   char mi[2];
  367.   tft.println("defina o minuto");
  368.   tft.print("de inicio :");
  369.    for (i=0; i< 2; i++){
  370.   char key = keypad.waitForKey();
  371.  
  372.    if((key != '*')&&(key!='#')&&(key!='A')&&(key!='B')&&(key!='C')&&(key!='D')){
  373.    
  374.    mi[i]=key;
  375.    tft.print(mi[i]);
  376.  
  377.    
  378.    }
  379.    else{
  380.    
  381.    i--;
  382.     }
  383.      tone(40, 1500, 30);
  384.      }
  385. minuto = atoi(mi);
  386. if (minuto >59){
  387.   tone(40, 666, 2000);
  388.   tft.println();
  389.   tft.println("minutos invalidos,");
  390.   tft.println("digite novamente.");
  391.   delay(3000);
  392.    minutoinicio(minuto);
  393.   }
  394.   Serial.println();
  395.   tft.fillScreen(BLACK);
  396.  
  397.  return minuto;
  398.  
  399.  
  400.  
  401.   }
  402.  
  403.  
  404.  
  405.  
  406.  
  407. int horafim(int &horaf){
  408.      tft.setCursor(0, 0);
  409.  tft.fillScreen(BLACK);
  410.  tft.setTextSize(3);
  411.   tft.setTextColor(WHITE);
  412.   int i;
  413.   char hr[2];
  414.    for (i=0; i< 2; i++){
  415.     hr[i]= "";
  416.  
  417.  
  418.    }
  419.    
  420.   tft.println("defina a hora de");
  421.   tft.println("fim de pico: ");
  422.    for (i=0; i< 2; i++){
  423.   char key = keypad.waitForKey();
  424.  
  425.    if((key != '*')&&(key!='#')&&(key!='A')&&(key!='B')&&(key!='C')&&(key!='D')){
  426.    hr[i]=key;
  427.    tft.print(hr[i]);
  428.  
  429.    
  430.    }
  431.    else{
  432.    
  433.    i--;
  434.     }
  435.     tone(40, 1500, 30);
  436.      }
  437. horaf = atoi(hr);
  438. if (horaf >23){
  439.   tone(40, 666, 2000);
  440.  tft.println();
  441.    tft.println("hora invalida,");
  442.    tft.println("digite novamente.");
  443.    delay(3000);
  444.    horafim(horaf);
  445.   }
  446.   tft.println();
  447.     tft.setCursor(0, 0);
  448.  tft.fillScreen(BLACK);
  449.  return horaf;
  450.  
  451.  
  452.  
  453.   }
  454. int minutofim(int &minutof){
  455.      tft.setCursor(0, 0);
  456.  tft.fillScreen(BLACK);
  457.  tft.setTextSize(3);
  458.   tft.setTextColor(WHITE);
  459.   int i;
  460.   char mi[2];
  461.   tft.println("defina o minuto");
  462.     tft.println("de fim de pico: ");
  463.    for (i=0; i< 2; i++){
  464.   char key = keypad.waitForKey();
  465.  
  466.    if((key != '*')&&(key!='#')&&(key!='A')&&(key!='B')&&(key!='C')&&(key!='D')){
  467.          
  468.    mi[i]=key;
  469.    tft.print(mi[i]);
  470.  
  471.    
  472.    }
  473.    else{
  474.    
  475.    i--;
  476.     }
  477.     tone(40, 1500, 30);
  478.      }
  479. minutof = atoi(mi);
  480. if (minutof >59){
  481.   tone(40, 666, 2000);
  482.  tft.println();
  483.   tft.println("minutos invalidos,");
  484.    tft.println("digite novamente.");
  485.   delay(3000);
  486.    minutofim(minutof);
  487.   }
  488.  
  489.   tft.setCursor(0, 0);
  490. tft.fillScreen(BLACK);
  491.  return minutof;
  492.  
  493.   }
  494.  
  495.  
  496.  
  497.  
  498. /* //parte amp
  499. void lecorrente(){
  500.  
  501.  RawValue = analogRead(analogIn);
  502.  Voltage = (RawValue / 1024.0) * 5000; // Gets you mV
  503.  Amps = ((Voltage - ACSoffset) / mVperAmp);
  504.  
  505.  
  506.  tft.print("Raw Value = " ); // shows pre-scaled value
  507.  tft.println(RawValue);
  508.  tft.print("mV = "); // shows the voltage measured
  509.  tft.println(Voltage,3); // the '3' after voltage allows you to display 3 digits after decimal point
  510.  tft.print("Amps = "); // shows the voltage measured
  511.  tft.println(Amps,3); // the '3' after voltage allows you to display 3 digits after decimal point
  512.  
  513.  
  514. }
  515.  
  516. */
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524. #define BUFFPIXEL 50
  525.  
  526. void bmpDraw(char *filename, int x, int y) {
  527.  
  528.   File     bmpFile;
  529.   int      bmpWidth, bmpHeight;   // W+H in pixels
  530.   uint8_t  bmpDepth;              // Bit depth (currently must be 24)
  531.   uint32_t bmpImageoffset;        // Start of image data in file
  532.   uint32_t rowSize;               // Not always = bmpWidth; may have padding
  533.   uint8_t  sdbuffer[3*BUFFPIXEL]; // pixel in buffer (R+G+B per pixel)
  534.   uint16_t lcdbuffer[BUFFPIXEL];  // pixel out buffer (16-bit per pixel)
  535.   uint8_t  buffidx = sizeof(sdbuffer); // Current position in sdbuffer
  536.   boolean  goodBmp = false;       // Set to true on valid header parse
  537.   boolean  flip    = true;        // BMP is stored bottom-to-top
  538.   int      w, h, row, col;
  539.   uint8_t  r, g, b;
  540.   uint32_t pos = 0, startTime = millis();
  541.   uint8_t  lcdidx = 0;
  542.   boolean  first = true;
  543.  
  544.   if((x >= tft.width()) || (y >= tft.height())) return;
  545.  
  546.   Serial.println();
  547.   progmemPrint(PSTR("Loading image '"));
  548.   Serial.print(filename);
  549.   Serial.println('\'');
  550.   // Open requested file on SD card
  551.   if ((bmpFile = SD.open(filename)) == NULL) {
  552.     progmemPrintln(PSTR("File not found"));
  553.      tft.setCursor(0, 0);
  554.   tft.setTextColor(GREEN);
  555.    tft.setTextSize(3);
  556.    tft.println("Nao");
  557.     tft.println("Encontrado");
  558.     return;
  559.   }
  560.  
  561.   // Parse BMP header
  562.   if(read16(bmpFile) == 0x4D42) { // BMP signature
  563.     progmemPrint(PSTR("File size: ")); Serial.println(read32(bmpFile));
  564.     (void)read32(bmpFile); // Read & ignore creator bytes
  565.     bmpImageoffset = read32(bmpFile); // Start of image data
  566.     progmemPrint(PSTR("Image Offset: ")); Serial.println(bmpImageoffset, DEC);
  567.     // Read DIB header
  568.     progmemPrint(PSTR("Header size: ")); Serial.println(read32(bmpFile));
  569.     bmpWidth  = read32(bmpFile);
  570.     bmpHeight = read32(bmpFile);
  571.     if(read16(bmpFile) == 1) { // # planes -- must be '1'
  572.       bmpDepth = read16(bmpFile); // bits per pixel
  573.       progmemPrint(PSTR("Bit Depth: ")); Serial.println(bmpDepth);
  574.       if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
  575.  
  576.         goodBmp = true; // Supported BMP format -- proceed!
  577.         progmemPrint(PSTR("Image size: "));
  578.         Serial.print(bmpWidth);
  579.         Serial.print('x');
  580.         Serial.println(bmpHeight);
  581.  
  582.         // BMP rows are padded (if needed) to 4-byte boundary
  583.         rowSize = (bmpWidth * 3 + 3) & ~3;
  584.  
  585.         // If bmpHeight is negative, image is in top-down order.
  586.         // This is not canon but has been observed in the wild.
  587.         if(bmpHeight < 0) {
  588.           bmpHeight = -bmpHeight;
  589.           flip      = false;
  590.         }
  591.  
  592.         // Crop area to be loaded
  593.         w = bmpWidth;
  594.         h = bmpHeight;
  595.         if((x+w-1) >= tft.width())  w = tft.width()  - x;
  596.         if((y+h-1) >= tft.height()) h = tft.height() - y;
  597.  
  598.         // Set TFT address window to clipped image bounds
  599.         tft.setAddrWindow(x, y, x+w-1, y+h-1);
  600.  
  601.         for (row=0; row<h; row++) { // For each scanline...
  602.           // Seek to start of scan line.  It might seem labor-
  603.           // intensive to be doing this on every line, but this
  604.           // method covers a lot of gritty details like cropping
  605.           // and scanline padding.  Also, the seek only takes
  606.           // place if the file position actually needs to change
  607.           // (avoids a lot of cluster math in SD library).
  608.           if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
  609.             pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
  610.           else     // Bitmap is stored top-to-bottom
  611.             pos = bmpImageoffset + row * rowSize;
  612.           if(bmpFile.position() != pos) { // Need seek?
  613.             bmpFile.seek(pos);
  614.             buffidx = sizeof(sdbuffer); // Force buffer reload
  615.           }
  616.  
  617.           for (col=0; col<w; col++) { // For each column...
  618.             // Time to read more pixel data?
  619.             if (buffidx >= sizeof(sdbuffer)) { // Indeed
  620.               // Push LCD buffer to the display first
  621.               if(lcdidx > 0) {
  622.                 tft.pushColors(lcdbuffer, lcdidx, first);
  623.                 lcdidx = 0;
  624.                 first  = false;
  625.               }
  626.               bmpFile.read(sdbuffer, sizeof(sdbuffer));
  627.               buffidx = 0; // Set index to beginning
  628.             }
  629.  
  630.             // Convert pixel from BMP to TFT format
  631.             b = sdbuffer[buffidx++];
  632.             g = sdbuffer[buffidx++];
  633.             r = sdbuffer[buffidx++];
  634.                        lcdbuffer[lcdidx++] = tft.color565(r,g/2,b);
  635.           } // end pixel
  636.         } // end scanline
  637.         // Write any remaining data to LCD
  638.         if(lcdidx > 0) {
  639.           tft.pushColors(lcdbuffer, lcdidx, first);
  640.         }
  641.         progmemPrint(PSTR("Loaded in "));
  642.         Serial.print(millis() - startTime);
  643.         Serial.println(" ms");
  644.       } // end goodBmp
  645.     }
  646.   }
  647.  
  648.   bmpFile.close();
  649.   if(!goodBmp) progmemPrintln(PSTR("BMP format not recognized."));
  650. }
  651.  
  652. // These read 16- and 32-bit types from the SD card file.
  653. // BMP data is stored little-endian, Arduino is little-endian too.
  654. // May need to reverse subscript order if porting elsewhere.
  655.  
  656. uint16_t read16(File f) {
  657.   uint16_t result;
  658.   ((uint8_t *)&result)[0] = f.read(); // LSB
  659.   ((uint8_t *)&result)[1] = f.read(); // MSB
  660.   return result;
  661. }
  662.  
  663. uint32_t read32(File f) {
  664.   uint32_t result;
  665.   ((uint8_t *)&result)[0] = f.read(); // LSB
  666.   ((uint8_t *)&result)[1] = f.read();
  667.   ((uint8_t *)&result)[2] = f.read();
  668.   ((uint8_t *)&result)[3] = f.read(); // MSB
  669.   return result;
  670. }
  671.  
  672. // Copy string from flash to serial port
  673. // Source string MUST be inside a PSTR() declaration!
  674. void progmemPrint(const char *str) {
  675.   char c;
  676.   while(c = pgm_read_byte(str++)) Serial.print(c);
  677. }
  678.  
  679. // Same as above, with trailing newline
  680. void progmemPrintln(const char *str) {
  681.   progmemPrint(str);
  682.   Serial.println();
  683. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement