Advertisement
RuiViana

Teste_7_Seg

Mar 23rd, 2018
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.44 KB | None | 0 0
  1. //  http://labdegaragem.com/forum/topics/display-7-segmentos-2
  2. #include "SevSeg.h"
  3. SevSeg myDisplay;               //Create an instance of the object.
  4. unsigned long timer;            //Create global variables
  5. int deciSecond = 9999;
  6.  
  7.  
  8.  
  9. //#include <LiquidCrystal.h>
  10. const int rs = 4, en = 5, d4 = 6, d5 = 7, d6 = 8, d7 = 9;
  11. //LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  12. unsigned long Inicio = 0; // variavel interia positiva para guardar o inicio da medida
  13. unsigned long finish = 0; // variavel interia positiva para guardar o fim da medida
  14. float distance = 10;      // variavel floatpoint para informar a distancia para medida
  15. // informar aqui a distancia entre os sensores em cm
  16. float interval;           // variavel floatpoint para guardar o calculo entre inicio e fim
  17. float tempsec;            // variavel floatpoint para guardar o tempo em segundos
  18. float mps;                // variavel floatpoint para guardar a velocidade em metros por segundos
  19. float fps;                // variavel floatpoint para guardar a velocidade em pés por segundo
  20. int done = 1;             // Flag para informar que foi feita a medida
  21. int FlagInicio = 0;       // Flag para informar que foi medido o tempo inicial
  22. int FlagFinal = 0;        // Flag para informar que foi medido o tempo final
  23. int startPin = 2;         // pino de entrada para sensor de inicio
  24. int finishPin = 3;        // pino de entrada para sensor de fim
  25. int LedGreen = A0;        // pino de saida para led verde
  26. int LedRed = A1;          // pino de saida para led vermelho
  27. // -----------------------------------------------
  28. void setup()
  29. {
  30.   //  lcd.begin(16, 2);
  31.   pinMode(startPin, INPUT_PULLUP);
  32.   pinMode(finishPin, INPUT_PULLUP);
  33.   pinMode(LedGreen, OUTPUT);
  34.   pinMode(LedRed, OUTPUT);
  35.   //  lcd.setCursor(3, 0);
  36.   //  lcd.print("CRONOGRAFO");        // Msg inicial
  37.   //  lcd.setCursor(1, 1);
  38.   //  lcd.print("Kalel  Santana");    // Msg inicial
  39.  
  40.   int displayType = COMMON_CATHODE; //Your display is either common cathode or common anode
  41.  
  42.   //This pinout is for OpenSegment PCB layout     //Declare what pins are connected to the digits
  43.  
  44.   int digit1 = 10; //Pin 10 on my 4 digit display
  45.   int digit2 = 11; //Pin 11 on my 4 digit display
  46.   int digit3 = 12; //Pin 12 on my 4 digit display
  47.   int digit4 = 13; //Pin 13 on my 4 digit display
  48.  
  49.   //Declare what pins are connected to the segments
  50.   int segA = 3; //Pin 2 on my 4 digit display
  51.   int segB = 4; //Pin 3 on my 4 digit display
  52.   int segC = 5; //Pin 4 on my 4 digit display
  53.   int segD = 6; //Pin 5 on my 4 digit display
  54.   int segE = 7; //Pin 6 on my 4 digit display
  55.   int segF = 2; //Pin 7 on my 4 digit display
  56.   int segG = 8; //Pin 8 on my 4 digit display
  57.   int segDP = 9; //Pin 9 on my 4 digit display
  58.  
  59.   int numberOfDigits = 4; //Do you have a 1, 2 or 4 digit display?
  60.   myDisplay.Begin(displayType, numberOfDigits, digit1, digit2, digit3, digit4, segA, segB, segC, segD, segE, segF, segG, segDP);
  61.   myDisplay.SetBrightness(100); //Set the display to 100% brightness level
  62.  
  63. }
  64. // -----------------------------------------------
  65. void loop()
  66. {
  67.   while (digitalRead(startPin) == LOW)    // Faça se pino startPin esta obstruido
  68.   {
  69.     if (FlagInicio == 0)                  // Faça se ainda não tem medida inicial
  70.     {
  71.       Inicio = millis();                  // Salva medida inicial em miliseg
  72.       FlagInicio = 1;                     // informa que já tem medida inicial
  73.     }
  74.   }
  75.   while (digitalRead(finishPin) == LOW)   // Faça se pino finishPin esta obstruido
  76.   {
  77.     if (FlagInicio == 1)                  // Faça se tem medida inicial
  78.     {
  79.       if (FlagFinal == 0)                 // Faça se ainda não tem medida final
  80.       {
  81.         finish = millis();                // Salva medida final em miliseg
  82.         done = 0;                         // informa que tudo foi medido
  83.         FlagFinal = 1;                    // informa que já tem medida final
  84.       }
  85.     }
  86.   }
  87.   while (!done)                           // faça se tudo foi medido
  88.   {
  89.     interval = float(finish - Inicio);    // calcula a diferença entre o inicio e o final em miliseg
  90.     tempsec = (interval / 1000);          //Converte o tempo para segundos
  91.     mps = (distance / tempsec) / 100;     // velocidade em metros por segundos
  92.     fps = (mps) * 3.28;                   // velocidade em pés por segundos
  93.     /*    lcd.clear();
  94.         lcd.setCursor(2, 0);
  95.         lcd.print("Velocidades:");
  96.         lcd.setCursor(0, 1);
  97.         lcd.print(fps, 2);
  98.         lcd.setCursor(4, 1);
  99.         lcd.print("FPS");
  100.         lcd.setCursor(8, 1);
  101.         lcd.print(mps, 2);
  102.         lcd.setCursor(12, 1);
  103.         lcd.print("M/S");
  104.     */
  105.     if (fps > 280)
  106.     {
  107.       digitalWrite(LedRed, HIGH);
  108.     }
  109.     else
  110.     {
  111.       digitalWrite(LedGreen, HIGH);
  112.       delay(1000);
  113.       digitalWrite(LedGreen, LOW);
  114.       digitalWrite(LedRed, LOW);
  115.     }
  116.     done = 1;                         // prepara para nova medida
  117.     FlagInicio = 0;                   // prepara para nova medida
  118.     FlagFinal = 0;                    // prepara para nova medida
  119.     finish = 0;                       // prepara para nova medida
  120.     Inicio = 0;                       // prepara para nova medida
  121.   }
  122.  
  123.   char tempString[10]; //Used for sprintf   //Example ways of displaying a decimal number
  124.   sprintf(tempString, "%4d", fps);   //Convert deciSecond into a string that is right adjusted
  125.   myDisplay.DisplayString(tempString, 3);   //(numberToDisplay, decimal point location)
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement