Re: Bluetooth přijímá jiné hodnoty, než posílám z telefonu
Napsal: 21 kvě 2019, 14:36
Stejně vůbec nechápu, proč dvě Arduina a dva BT moduly.
České fórum pro všechny nadšence do Arduina a dalších technologií.
https://forum.hwkitchen.cz/
Potřebuju vynulovat proměnou speedk v případě, že její hodnota se například tři vteřiny nezměnila. Pokud tedy speedk bude mít hodnotu 60 celé tři vteřiny, potřebuji, aby se vynulovala...
Kód: Vybrat vše
//Master
#define PIN_IRQ 2
long start;
long elapsed;
float circMetric = 2.093; // wheel circumference (in meters)
volatile int speedk;
void setup() {
Serial1.begin(38400); // Default communication rate of the Bluetooth module
Serial.begin(38400);
pinMode(PIN_IRQ,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(PIN_IRQ), speedCalc, RISING);
start = millis();
}
void speedCalc() {
//Function called by the interrupt
if ((millis() - start) > 100) // 100 millisec debounce{
//calculate elapsed
elapsed = millis() - start;
//reset start
start = millis();
//calculate speed in km/h
speedk = (int)((3600.0 * circMetric) / (float)elapsed);
}
void loop() {
Serial.print("Odesilam na Slave : ");
Serial.println(speedk);
Serial1.write(speedk);
delay(100);
}
Kód: Vybrat vše
//Slave
#include <SoftwareSerial.h>
#define Aura 3
#define TIMEOUT 2500 // doba cekani na data od Master v ms
int State = 0; // Vyctena hodnota z bluetooth
int OldState = 0;
int CountTheSame = 0;
unsigned long LastTime;
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
pinMode(Aura, OUTPUT);
mySerial.begin(38400);
Serial.begin(38400);
analogWrite(Aura, 0);
LastTime = millis();
}
void loop() {
if (mySerial.available() > 0) {
LastTime = millis();
State = mySerial.read();
Serial.print("Prijato z Master : " );
Serial.println(State);
// pokud prijdou po sobe dva stejne udaje zvysi se citac CountTheSame
if (OldState == State) CountTheSame++; else CountTheSame = 0;
OldState = State;
// pokud prijdou 20x po sobe stejne udaje nebo budou mimo rozsah 0-255, nastavi se na vystup PWM 0
if (State < 0 || State > 255 || CountTheSame > 20)State = 0;
analogWrite(Aura, State);
Serial.print("Odeslano na PWM : ");
Serial.println(State);
}
// Nulovani, pokud prestanou prichazet data od Master
if (millis() - LastTime > TIMEOUT) {
State = 0;
analogWrite(Aura, State);
LastTime=millis();
Serial.println("Nedostavam Data !");
Serial.print("Odeslano na PWM : ");
Serial.println(State);
}
}