Advertisement
tuliobaars

eggtimer

Jun 16th, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.05 KB | None | 0 0
  1. int potpin = 0;
  2. int ledPin = 13;
  3. int Botao = 2; //botao no pino 2
  4. int EstadoBotao = 0; //Variavel para ler o status do pushbutton
  5. int potvalor = 0;
  6. int potvalorfinal = 0;
  7. int minutos = 0;
  8. int segundostotal = 0;
  9. int segundos = 0;
  10. int Buzzer = 10;
  11. int minutosfinal = 0;
  12. int nbrshow = 0;
  13. int nbratual = 0;
  14.  
  15. String stringmin, stringsec, stringtotal;
  16.  
  17. const int  g_pinCommLatch = 6;
  18. const int  g_pinClock     = 7;
  19. const int  g_pinData    = 4;
  20. byte g_digits [10];
  21. int g_numberToDisplay = 0;
  22. const int g_registers = 3;
  23.  
  24.  
  25. byte g_registerArray [g_registers];
  26.  
  27. void setup(){
  28.   pinMode(ledPin, OUTPUT); //Pino do led será saída
  29.   pinMode(Botao, INPUT); //Pino com botão será entrada
  30.   pinMode(Buzzer, OUTPUT);
  31.   pinMode (g_pinCommLatch, OUTPUT);
  32.   pinMode (g_pinClock, OUTPUT);
  33.   pinMode (g_pinData, OUTPUT);
  34.   digitalWrite(Botao, HIGH); //Ativa Pull-up
  35.   Serial.begin(9600);
  36.   g_digits [0] = 8 + 4 + 2 + 64 + 32 + 1;
  37.   g_digits [1] = 4 + 2;
  38.   g_digits [2] = 8 + 4 + 16 + 32 + 64;
  39.   g_digits [3] = 8 + 4 + 16 + 2 + 64;
  40.   g_digits [4] = 1 + 16 + 4 + 2;
  41.   g_digits [5] = 8 + 1 + 16 + 2 + 64;
  42.   g_digits [6] = 8 + 1 + 16 + 2 + 64 + 32;
  43.   g_digits [7] = 8 + 4 + 2;
  44.   g_digits [8] = 8 + 4 + 2 + 64 + 32 + 1 + 16;
  45.   g_digits [9] = 8 + 4 + 2 + 1 + 16 + 64;
  46. }
  47. void sendSerialData (
  48. byte registerCount,  // How many shift registers?
  49. byte *pValueArray)   // Array of bytes with LSByte in array [0]
  50. {
  51.   // Signal to the 595s to listen for data
  52.   digitalWrite (g_pinCommLatch, LOW);
  53.  
  54.   for (byte reg = registerCount; reg > 0; reg--)
  55.   {
  56.     byte value = pValueArray [reg - 1];
  57.  
  58.     for (byte bitMask = 128; bitMask > 0; bitMask >>= 1)
  59.     {
  60.       digitalWrite (g_pinClock, LOW);
  61.  
  62.       digitalWrite (g_pinData, value & bitMask ? HIGH : LOW);
  63.  
  64.       digitalWrite (g_pinClock, HIGH);
  65.     }
  66.   }
  67.   // Signal to the 595s that I'm done sending
  68.   digitalWrite (g_pinCommLatch, HIGH);
  69. }
  70. void countdown(){
  71.   while (segundos > 0 || minutos > 0){
  72.     delay(1000);
  73.     Serial.print("Tempo: ");
  74.     Serial.print(minutos);
  75.     Serial.print(":");
  76.     Serial.println(segundos);
  77.     segundos = segundos--;
  78.     nbratual = (minutos * 100) + segundos;
  79.     g_numberToDisplay = nbratual;
  80.  
  81.     if (g_numberToDisplay < 10)
  82.     {
  83.       g_registerArray [3] = g_digits [0];
  84.       g_registerArray [2] = g_digits [0];
  85.       g_registerArray [1] = g_digits [0];
  86.       g_registerArray [0] = g_digits [g_numberToDisplay];
  87.     }
  88.     else if (g_numberToDisplay < 100)
  89.     {
  90.       g_registerArray [3] = g_digits [0];
  91.       g_registerArray [2] = g_digits [0];
  92.       g_registerArray [1] = g_digits [g_numberToDisplay / 10];
  93.       g_registerArray [0] = g_digits [g_numberToDisplay % 10];
  94.     }
  95.     else if (g_numberToDisplay < 1000)
  96.     {
  97.       g_registerArray [3] = g_digits [0];
  98.       g_registerArray [2] = g_digits [g_numberToDisplay / 100];
  99.       g_registerArray [1] = g_digits [(g_numberToDisplay % 100) / 10];
  100.       g_registerArray [0] = g_digits [g_numberToDisplay % 10];
  101.     }
  102.     else
  103.     {
  104.       g_registerArray [3] = g_digits [g_numberToDisplay / 1000];
  105.       g_registerArray [2] = g_digits [(g_numberToDisplay % 1000) / 100];
  106.       g_registerArray [1] = g_digits [(g_numberToDisplay % 100) / 10];
  107.       g_registerArray [0] = g_digits [g_numberToDisplay % 10];
  108.     }
  109.     sendSerialData (g_registers, g_registerArray);
  110.     if (segundos == 0){
  111.       if (minutos > 0){
  112.         minutos = minutos--;
  113.         segundos = 60;
  114.       }
  115.       else {
  116.         Serial.println("Acabou!!");
  117.         digitalWrite(ledPin, HIGH);
  118.         digitalWrite(Buzzer, HIGH);
  119.         delay(500);
  120.         digitalWrite(Buzzer, LOW);
  121.         delay(500);
  122.         digitalWrite(Buzzer, HIGH);
  123.         delay(500);
  124.         digitalWrite(Buzzer, LOW);
  125.         delay(500);
  126.         digitalWrite(Buzzer, HIGH);
  127.         delay(500);
  128.         digitalWrite(Buzzer, LOW);
  129.         delay(500);
  130.         digitalWrite(Buzzer, HIGH);
  131.         delay(500);
  132.         digitalWrite(Buzzer, LOW);
  133.       }
  134.     }
  135.   }
  136. }
  137.  
  138. void loop(){
  139.   EstadoBotao = digitalRead(Botao);
  140.   potvalor = map(analogRead(potpin), 0, 1023, 1, 301);
  141.   segundostotal = potvalor;
  142.   minutos = segundostotal / 60;
  143.   segundos = segundostotal - (minutos*60);
  144.   minutosfinal = minutos * 100;
  145.   nbrshow = minutosfinal + segundos;
  146.   g_numberToDisplay = nbrshow;
  147.  
  148.     if (g_numberToDisplay < 10)
  149.     {
  150.       g_registerArray [3] = g_digits [0];
  151.       g_registerArray [2] = g_digits [0];
  152.       g_registerArray [1] = g_digits [0];
  153.       g_registerArray [0] = g_digits [g_numberToDisplay];
  154.     }
  155.     else if (g_numberToDisplay < 100)
  156.     {
  157.       g_registerArray [3] = g_digits [0];
  158.       g_registerArray [2] = g_digits [0];
  159.       g_registerArray [1] = g_digits [g_numberToDisplay / 10];
  160.       g_registerArray [0] = g_digits [g_numberToDisplay % 10];
  161.     }
  162.     else if (g_numberToDisplay < 1000)
  163.     {
  164.       g_registerArray [3] = g_digits [0];
  165.       g_registerArray [2] = g_digits [g_numberToDisplay / 100];
  166.       g_registerArray [1] = g_digits [(g_numberToDisplay % 100) / 10];
  167.       g_registerArray [0] = g_digits [g_numberToDisplay % 10];
  168.     }
  169.     else
  170.     {
  171.       g_registerArray [3] = g_digits [g_numberToDisplay / 1000];
  172.       g_registerArray [2] = g_digits [(g_numberToDisplay % 1000) / 100];
  173.       g_registerArray [1] = g_digits [(g_numberToDisplay % 100) / 10];
  174.       g_registerArray [0] = g_digits [g_numberToDisplay % 10];
  175.     }
  176.     sendSerialData (g_registers, g_registerArray);
  177.  
  178.   if (EstadoBotao == HIGH){ //Se botão estiver pressionado (LOW)
  179.     digitalWrite(ledPin, HIGH); // acende o led do pino 13.
  180.     potvalorfinal = potvalor;
  181.     delay(500);
  182.  
  183.     segundostotal = potvalorfinal;
  184.     minutos = segundostotal / 60;
  185.     segundos = segundostotal - (minutos*60);
  186.  
  187.     Serial.print("analogRead: ");
  188.     Serial.println(analogRead(potpin));
  189.  
  190.     Serial.print("potvalor: ");
  191.     Serial.println(potvalor);
  192.  
  193.     Serial.print("Segundos: ");
  194.     Serial.println(segundostotal);
  195.     Serial.println();
  196.  
  197.     Serial.print("Tempo: ");
  198.     Serial.print(minutos);
  199.     Serial.print(":");
  200.     Serial.println(segundos);
  201.  
  202.     countdown();
  203.   }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement