Skocz do zawartości

Dalmierz Bluetooth - problem z kodem


MrReus

Pomocna odpowiedź

Witam. Tworzę projekt, którym miałby być rozbudowany dalmierz z modułem Bluetooth lecz mam problem z kodem.  Mianowicie chciałem, aby na module Keypad Shield wyświetlało się menu wyboru miejsca wyświetlania odległości - czy na ekranie czy przesyłanie na Bluetooth. Stworzyłem też prostą aplikację do wyświetlania tej odległości na ekranie smartfonu, i do tej pory wszystko było w porządku, lecz wpadłem na pomysł, żeby po podłączeniu się po bluetooth rozszerzyć możliwości dalmierza.

Chciałem dodać możliwość zmiany jednostki z cm na cale i tutaj się zatrzymałem.

#include <SoftwareSerial.h> 
#include <LiquidCrystal.h>
SoftwareSerial Bluetooth(2, 3);

#define trigPin 13
#define echoPin 11

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int lcd_key   = 0;
int button  = 0;
int zmiana = 0;

#define RIGHT  0
#define UP     1
#define DOWN   2
#define LEFT   3
#define SELECT 4
#define NONE   5

int Keypad_buttons() {
  button = analogRead(0);
  if (button > 1000) return NONE;
  if (button < 50)   return RIGHT;
  if (button < 250)  return UP;
  if (button < 450)  return DOWN;
  if (button < 600)  return LEFT;
  if (button < 920)  return SELECT;
  return NONE;
}


void bt_rangefinder(){
    lcd.clear();
    lcd.print("Bluetooth ");
    lcd.setCursor(0, 1);
    lcd.print("Rst: Reset"); 
    while(1)
  {
    float czas, odleglosc;
    digitalWrite(trigPin, LOW); 
    delayMicroseconds(2);
 
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
  
    czas = pulseIn(echoPin, HIGH);
    odleglosc = (czas / 2) * 0.0344;


    if (odleglosc >= 400 || odleglosc <= 2){
      Bluetooth.print("Odleglość = ");
      Bluetooth.println("Poza zasięgiem");
    
  }
    else { 
      Bluetooth.print("Odleglość = ");
      Bluetooth.print(odleglosc);
      Bluetooth.println(" cm");
      delay(500);
      } 
      delay(500);
  } 
}

void lcd_rangefinder(){
  while(true)
  {
    float czas, odleglosc;
    digitalWrite(trigPin, LOW); 
    delayMicroseconds(2);
 
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
  
    czas = pulseIn(echoPin, HIGH);
    odleglosc = (czas / 2) * 0.0344;
    lcd.clear();
    if (odleglosc >= 400 || odleglosc <= 2){
    lcd.print("Odl = ");
    lcd.print("Poza zasięgiem");
  }
  else {
    lcd.print("Odl = ");
    lcd.print(odleglosc);
    lcd.print(" cm");
    lcd.setCursor(0, 1);
    lcd.print("Rst: Reset");
    delay(500);
  }
    delay(500);
  }
  }


void setup() {
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Right: Bluetooth");
  lcd.setCursor(0,1);
  lcd.print("Left: LCD");
 
  Serial.begin (9600);
  Bluetooth.begin(9600); 
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {  
  lcd.setCursor(9, 1);
  lcd.setCursor(0, 1);
  lcd_key = Keypad_buttons();
  zmiana = Bluetooth.read();
  Serial.println(zmiana);
  switch (lcd_key) {
    case RIGHT: {
      bt_rangefinder();
      break; 
      } 
      
    case LEFT: {
      lcd_rangefinder();
      break;
    }
  }
  }

Stworzyłem funkcję do wyświetlania na ekranie oraz na bluetooth a w aplikacji dodałem przyciski, które wysyłają dane(0,1).

Chciałem dodać linie kodu, które po odczytaniu z Bluetooth odpowiedniej danej uruchomią odpowiednią funkcję, ale mam z tym problem.

Gdy dodałem:

zmiana = Bluetooth.read();
if (zmiana == '1')
{
}
else if (zmiana == '0')
{
}

nic się nie dzieje. Czy jest ktoś w stanie pomóc?

Link do komentarza
Share on other sites

Bądź aktywny - zaloguj się lub utwórz konto!

Tylko zarejestrowani użytkownicy mogą komentować zawartość tej strony

Utwórz konto w ~20 sekund!

Zarejestruj nowe konto, to proste!

Zarejestruj się »

Zaloguj się

Posiadasz własne konto? Użyj go!

Zaloguj się »
×
×
  • Utwórz nowe...