Honestly I couldn't get you ...
...do you mean that you want to have 2 variables to store 2 input values (from BT) to implement the filter on AI2 ?
I believe it is that. Well here it is, I want to know(to show on screen if bt module gets disconnected physicaly. I read that it is not possible unless I constantly send data to the app. And since I already have ones and zeroes to receive I want to somehow filter them for different purposes. Please don't give a solution this time, I would like to try it myself:rofl:
Ah ah ah,
ok I will not tell you that to avoid the disconnection my solution (it is already retrievable from the FAQ) is to make the Arduino sending periodically a "live" character (i.e. a '$'), for example every 1 minute. The app, when receives a '$', resets a timeout clock whose period is, for example 2 minutes. If, at the next period of 2 minutes, the clock fires without having received the $, this means that the BT has diconnected (no $ => no BT communicatoin active). At that moment the app retries a re-connection.
Of course every time you receive a '1' or a '0' the app timeout clock shall be reloaded as well.
This is the most simple, but reliable, way to discover a disconnection. Now is up to you to implement it
PS in other words: at every character received (i.e. '1' or '0' on event and '$' periodically), the timeout clock is stopped, its period is reloaded to 2000, then it is restarted. So it shall never fire. If it fires, this means that the BT has ceased to receive.
I tried the method they said was their favourite for debouncing and change it for my purposes but it does not print 0 after I click the switch, I used just Serial.print to see where is the problem. Here is the full so you canmore easily read:
#include <SoftwareSerial.h>
#define TxPin 11 // required by SoftwareSerial (PWM capabilities needed)
#define RxPin 10 // required by SoftwareSerial (PWM capabilities needed)
#define el_magnet 7
#define command 8
#define debug // comment it out at the end of the debug phase (no Serial Monitor Tx)
bool el_magON= false;
unsigned long prethodno_vrijeme=0;
const long interval=3000;
SoftwareSerial BTSerial(RxPin,TxPin); // instantiate the library
char state = ' '; // define input buffer as char (not empty but a blank)
const int stepPin = 4;
const int directionPin = 2;
unsigned long T1 = 0, T2 = 0;
uint8_t TimeInterval = 5; // Time interval for debounce mechanism (in milliseconds)
uint16_t BtnStates = 0;
int previousState = -1; // Variable to store the previous state of the button pin
//-----------------------------------------------------------------------------------------------
void zatvori()
{
digitalWrite(directionPin,HIGH); //npr zatvori
StepperActuate();
}
//-----------------------------------------------------------------------------------------------
void otvori()
{
digitalWrite(directionPin,LOW); //npr otvori
StepperActuate();
}
//-----------------------------------------------------------------------------------------------
void StepperActuate()
{
for(int x = 0; x < 50; x++)
{
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
}
//===============================================================================================
void setup() // done once at reset
{
pinMode(el_magnet,OUTPUT);
digitalWrite(el_magnet,LOW);
pinMode(command,INPUT_PULLUP);
pinMode(stepPin,OUTPUT);
pinMode(directionPin,OUTPUT);
// not necessary, already set into the library => pinMode(RxPin,INPUT); // to be connected to Tx pin of HC05
// not necessary, already set into the library => pinMode(TxPin,OUTPUT); // to be connected to Rx pin of HC05
BTSerial.begin(9600); // supposed baudrate of HC05 (38400 ?)
Serial.begin(9600); // Used to echo on PC monitor the data received on BT
}
//===============================================================================================
void loop() // repeated forever
{
T2 = millis();
if( (T2 - T1) >= TimeInterval )
{
BtnScan();
T1 = T2; // Update T1 after BtnScan() is called
}
static char character = 'A'; // Character to print (static to retain its value across loop iterations)
static unsigned long lastPrintTime = 0; // Variable to store the last time character was printed
unsigned long currentTime = millis(); // Get the current time
if (currentTime - lastPrintTime >= 60000) { // Check if 2 seconds have elapsed
Serial.println(character); // Print character
lastPrintTime = currentTime; // Update last print time
}
// while((millis()-now) <100UL) ; removed because the BT tx is done only when the input changes
if (BTSerial.available()) // any char from BT ?
{
state = BTSerial.read(); // yes, get it
#ifdef debug
Serial.print("Received : "); // echoes it on the PC screen only if the debug flag is defined (see header)
Serial.println(state);
#endif
if(state=='2') //npr zatvori
{
zatvori();
}
if(state=='3') //npr otvori
{
otvori();
}
if(state=='4') {// the received char to switch on the led
digitalWrite(el_magnet,LOW);
el_magON=false;
}
if(state=='5') {
if (!el_magON) {
digitalWrite(el_magnet, HIGH);
el_magON=true;
prethodno_vrijeme=millis();
}
}
}
unsigned long trenutno_vrijeme=millis();
if (el_magON && (trenutno_vrijeme-prethodno_vrijeme>=interval)){
digitalWrite(el_magnet, LOW);
el_magON=false;
}
}
void BtnScan(void) {
int currentState = digitalRead(command);
int SumStates = 0;
BtnStates <<= 1;
BtnStates |= currentState;
SumStates = __builtin_popcount(BtnStates);
if(SumStates >= 10)
{
if (currentState != previousState) {
Serial.println(currentState);
previousState = currentState; // Update the previous state
}
BtnStates = 0;
}
}
Dear @Bruno_Dragas,
I'll be out for a couple of days. I'll give it a sight on Monday, unless you find the issue by yourself before.
Have a great WE.
Alright, thanks have a good one too
Hello @Bruno_Dragas,
though out for the weekend, I was curious to see your code, therefore I've had a quick sight to it.
In that code you've missed the serialBT.println(ToBeTx);
In other words, this:
So, nothing is transmitted, whatever the debounce is.
Well I removed BT part in order to see whether it would transmit it via serial monitor to reduce the potential problem source. I ,abandonded' that code and used another which works fine now with no random bounces. But recent events proved me as jinxed haha so I will keep my mouth shut😁. I really appreciate your determination! Thanks🥰
Ok, glad it works, finally
If I understood your clock timer idea of constantly receiving data. So after the 0, 1 or periodically sent character are sent I set clock timer to disabled and set some interval(2seconds for e.g.) and then enable it. Then I can put a if statement saying if it is enabled then set label textx "bt disconnected" Since everytime the waiting interval ends new data will be sent so there won't be time for clock to be firing. If it is firing it means no data is being received(bt disconnected).
Here is the code I modfiied based on how I interpreted your post, I guess I need a clock reload too? Also I guess I forgot to set clock2 timer specs in after picking listpicker blocks
No worries, I was not able to send images that is why it looks like i spammed the messages. Sorry for inconve nience
Dear @Bruno_Dragas ,
I have no time to write for you a new complete code, but your blocks are close to the solution.
On AI2 side.
Clock 2 can be a very slow clock, for example the time interval can be one or two minutes.
It shall be initialized only after a BT connection has been established successfully.
In clock 1 at every receving event (every time it receives something: '0', '1', '$',....) you shall set to true the "live" boolean variable.
Clock 2 checks for the live flag to be true, if it is found true, it is set to false.
If it is found false, it rises the alarm and retries to connect (or it aborts the app, whatever you want to be the recovery action).
On Arduino side.
In your loop funcrion, you shall send every 1 minute the "live" character (I suggest to use a character that is not sent during "normal transmission", typically a special character, like $ or & or %...).
To achieve the 1 minute period don't use the delay(60000UL); function otherwise your code will remain stuck while waiting, use the millis() instead..
As a global variable
unsigned long minutes = 0UL;
in setup()
{
....
....
minutes = millis();
}
in loop()
{
.....
.....
if ((millis() - minutes) >= 60000UL) {SeriaBT.println('$'); minutes = millis();}
....
.....
}
Have a nice coding
Well arduino part I figured haha. I didn't use delay as you said, it would put the full code in waiting position. The AI2 part, I will get into it. Thanks for your time.
Should I put the disconnect on error or disconnect block or is it enough to just set label text to disconnected, since if it was unplugged it is already disconnected:grin:
In my experience I've never used the disconnect on error.
On the opposite, when the disconnection is discovered by the APP, I try to reconnect but very rarely it happens successfully without a human intervention: i.e. resetting the Arduino device or the Android one.
And if my blocks are the right ones, I should get an bt status disconnected label text (in =/under 1 minute if i set the clock 2 timer at 60 000) if i unplug arduino?
Yep! No '$' no BT