Calculadora Bluetooth

De Wiket
Salta a la navegació Salta a la cerca

Requeriments

  • Placa Arduino Uno
  • Bluetooth (Preferiblement HC-06)
  • 7 Segment Display
  • Protoboard
  • 4 (Bluetooth) + 8 (7 Segment Display) cables
  • Una Resistencia (3 Bandes: Taronja, Blau, Marró; Tolerància: Or)

Esquema

Fitxer:Davidsúbeloyalento.png

Codi

#include <SoftwareSerial.h>
#define segA 2
#define segB 3
#define segC 4
#define segD 5
#define segE 6
#define segF 7
#define segG 8

SoftwareSerial BT1(0,1); 

void setup(){

    Serial.begin(9600);
    BT1.begin(9600);

    for (int i=2;i<9;i++){
        pinMode(i, OUTPUT);
    }
}

void loop(){

      char c;
      char b;
      int n1=10;
      int n2=10;
      int resultat;
    
      if (BT1.available()){
          do{
            
            do{
              c = BT1.read();
            }while(c == NULL);
            
            if(isDigit(c)){
               String S1 = String(c);
               n1 = S1.toInt();
            }
            
          }while(n1>9 || n1<0);

          do{
            
            do{
              b = BT1.read();
            }while(b == NULL);
            
            if(isDigit(b)){
               String S2 = String(b);
               n2 = S2.toInt();
            }
            
          }while(n2>9 || n2<0);
               
          resultat = n1 + n2;

          switch(resultat){
          
              case 0:
                    digitalWrite(segA, HIGH);
                    digitalWrite(segB, HIGH);
                    digitalWrite(segC, HIGH);
                    digitalWrite(segD, HIGH);
                    digitalWrite(segE, HIGH);
                    digitalWrite(segF, HIGH);
                    digitalWrite(segG, LOW);
                    break;
               
               case 1:
                    digitalWrite(segA, LOW);
                    digitalWrite(segB, HIGH);
                    digitalWrite(segC, HIGH);
                    digitalWrite(segD, LOW);
                    digitalWrite(segE, LOW);
                    digitalWrite(segF, LOW);
                    digitalWrite(segG, LOW);
                    break;
               
               case 2:
                    digitalWrite(segA, HIGH);
                    digitalWrite(segB, HIGH);
                    digitalWrite(segC, LOW);
                    digitalWrite(segD, HIGH);
                    digitalWrite(segE, HIGH);
                    digitalWrite(segF, LOW);
                    digitalWrite(segG, HIGH);
                    break;
               
               case 3:
                    digitalWrite(segA, HIGH);
                    digitalWrite(segB, HIGH);
                    digitalWrite(segC, HIGH);
                    digitalWrite(segD, HIGH);
                    digitalWrite(segE, LOW);
                    digitalWrite(segF, LOW);
                    digitalWrite(segG, HIGH);
                    break;
               
               case 4:
                    digitalWrite(segA, LOW);
                    digitalWrite(segB, HIGH);
                    digitalWrite(segC, HIGH);
                    digitalWrite(segD, LOW);
                    digitalWrite(segE, LOW);
                    digitalWrite(segF, HIGH);
                    digitalWrite(segG, HIGH);
                    break;
               
               case 5:
                    digitalWrite(segA, HIGH);
                    digitalWrite(segB, LOW);
                    digitalWrite(segC, HIGH);
                    digitalWrite(segD, HIGH);
                    digitalWrite(segE, LOW);
                    digitalWrite(segF, HIGH);
                    digitalWrite(segG, HIGH);
                    break;
               
               case 6:
                    digitalWrite(segA, HIGH);
                    digitalWrite(segB, LOW);
                    digitalWrite(segC, HIGH);
                    digitalWrite(segD, HIGH);
                    digitalWrite(segE, HIGH);
                    digitalWrite(segF, HIGH);
                    digitalWrite(segG, HIGH);
                    break;  
               
               case 7:
                    digitalWrite(segA, HIGH);
                    digitalWrite(segB, HIGH);
                    digitalWrite(segC, HIGH);
                    digitalWrite(segD, LOW);
                    digitalWrite(segE, LOW);
                    digitalWrite(segF, LOW);
                    digitalWrite(segG, LOW);
                    break;
               
               case 8:
                    digitalWrite(segA, HIGH);
                    digitalWrite(segB, HIGH);
                    digitalWrite(segC, HIGH);
                    digitalWrite(segD, HIGH);
                    digitalWrite(segE, HIGH);
                    digitalWrite(segF, HIGH);
                    digitalWrite(segG, HIGH);
                    break;
               
               case 9:
                    digitalWrite(segA, HIGH);
                    digitalWrite(segB, HIGH);
                    digitalWrite(segC, HIGH);
                    digitalWrite(segD, HIGH);
                    digitalWrite(segE, LOW);
                    digitalWrite(segF, HIGH);
                    digitalWrite(segG, HIGH);
                    break;    
                               
               defaul:
                    Serial.write("Error, el resultat ha de trobar-se entre 0-9");
                    break;
          }
      }
       
   delay(200);
}

Imatges

    Display.png