Arduino code - momentary delay

I have arduino sketch, android app and hardware with atmega328p. The app communicates with the atmega via hm10 BLE. App was working fine, but recently needed to update BLE libraries. Now when I click on a momentary button in the app, if I press and release too fast, the relay that is being triggered only registers the initial on press and not the off. In serial monitor it reads as if it sent both commands, but only really sent the first command.

Question: Is this an issue with the code? or did something happen in the app programming/ library update? Code has not changed. I had someone else update the library in the app. Please let me know your thoughts? Is this a common problem?

You need to write a "pulse stretcher" for the relay/trigger. That is to say, if a relay receives any trigger, the sketch should not release that relay/trigger until the relay has registered as you describe.

Why did you need to update the BLE libraries? Were those the Arduino libraries or something on the cell phone?

Apologies in advance, but I am a newbie to programming. I wrote the arduino sketch, and built the original app using MIT app inventor, then sent it over to a programmer to complete.
Is that part of the app code or the arduino sketch?

The app was made about 5 years ago and hasn't been updated since. It worked fine last time I used it about 6 months ago and when I went to use it recently, the app would not connect with the BLE. The programmer I am using to help create the app said that the libraries needed to be updated. The libraries on the app I assume.

Arduino sketch. (there are hardware solutions with resistor/capacitor timing, but software works well).

The idea is to recognize when the button is pressed, and set a timer, usually with millis().
Normally, if you release the button, the sketch is told to stop energizing the relay.
The new way would be to let the button be release, but only release the relay when the timer has counted long enough for the rest of the sketch to "see and acknowledge" the relay was on (then off).

You must not assume; ask the programmer what he/she meant so you know.

Is the Serial monitor connected to :

  • your arduino
    or
  • direct connected to hm10 BLE-bluetooth-adapter
    or
  • connected to Android-App

In case your arduino is connected to the serial monitor.

If your arduino-code receives both

How could the receiving code print out
"ON-command received"
and
"OFF-command received"

If the transmitter does not send the
OFF-command????

I guess you have to post a better and more detailed description of what is happening.

best regards Stefan

This is a bit above my paygrade in terms of code writting. Here is the arduino sketch that I have. The interesting thing is that it was working just fine previously which makes me think that it is something on the app side and not the sketch. Can you kindly take a look at the code below and let me know what you think?

int Actuator_A_Extend = 2;   
int Actuator_A_Contract = 3;

int Actuator_B_Extend = 4;
int Actuator_B_Contract = 5;

int Actuator_C_Extend = 6;
int Actuator_C_Contract = 7;

int Actuator_D_Extend = 8;
int Actuator_D_Contract = 9;

int Actuator_E_Extend = 14; 
int Actuator_E_Contract = 15;


int del = 4500;
int tdel;
bool change = false;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  digitalWrite(Actuator_A_Extend, HIGH);
  digitalWrite(Actuator_A_Contract, HIGH); 
  digitalWrite(Actuator_B_Extend, HIGH); 
  digitalWrite(Actuator_B_Contract, HIGH); 
  digitalWrite(Actuator_C_Extend, HIGH); 
  digitalWrite(Actuator_C_Contract, HIGH); 
  digitalWrite(Actuator_D_Extend, HIGH); 
  digitalWrite(Actuator_D_Contract, HIGH); 
  digitalWrite(Actuator_E_Extend, HIGH); 
  digitalWrite(Actuator_E_Contract, HIGH); 
   
  pinMode(Actuator_A_Extend, OUTPUT);
  pinMode(Actuator_A_Contract, OUTPUT);
  pinMode(Actuator_B_Extend, OUTPUT);
  pinMode(Actuator_B_Contract, OUTPUT);
  pinMode(Actuator_C_Extend, OUTPUT);
  pinMode(Actuator_C_Contract, OUTPUT);
  pinMode(Actuator_D_Extend, OUTPUT);
  pinMode(Actuator_D_Contract, OUTPUT);
  pinMode(Actuator_E_Extend, OUTPUT);
  pinMode(Actuator_E_Contract, OUTPUT);
}

void loop() {

  if(!Serial.available())
  if(change){
    change = false;
    del = tdel;
  }
  if(Serial.available() > 0){
    char data;
    data = Serial.read();
    Serial.print(data);
    
    if(data == 'X')
    {
      change = true;
      while(!Serial.available()){delay(5);}
      tdel = 0;
      int count = 0;
      while(count<4){
        count++;
        if(Serial.available()>0)
        {
          data = Serial.read();
          tdel *=10;
          tdel += data - '0';
          delay(5);
        }
        else
          break;
      }
    }
    else
    switch (data)
    {
      case '1': //ACTUATOR B Contract
        digitalWrite(Actuator_B_Extend, HIGH);
        digitalWrite(Actuator_B_Contract, LOW);
        break;
        
      case '2': //ACTUATOR B Extend
        digitalWrite(Actuator_B_Contract, HIGH);
        digitalWrite(Actuator_B_Extend, LOW);
        break;
        
      case '3': //ACTUATOR B Extend Delay Variable Actuator D Extend Actuator A+C Contract Delay 4500
        digitalWrite(Actuator_A_Extend, HIGH);
        digitalWrite(Actuator_A_Contract, LOW);
        digitalWrite(Actuator_B_Extend, LOW);
        digitalWrite(Actuator_B_Contract, HIGH);
        digitalWrite(Actuator_C_Extend, HIGH);
        digitalWrite(Actuator_C_Contract, LOW);
        digitalWrite(Actuator_D_Extend, LOW);
        digitalWrite(Actuator_D_Contract, HIGH);
        delay(del);
        digitalWrite(Actuator_B_Extend, HIGH);
        delay((4500)-(del));
        digitalWrite(Actuator_D_Extend, HIGH);
        digitalWrite(Actuator_A_Contract, HIGH);
        digitalWrite(Actuator_C_Contract, HIGH);
        break;
        
     case '4': //ACTUATOR A Extend Delay Variable Actuator C Extend Actuator B+D Contract Delay 4500
        digitalWrite(Actuator_A_Extend, LOW);
        digitalWrite(Actuator_A_Contract, HIGH);
        digitalWrite(Actuator_B_Extend, HIGH);
        digitalWrite(Actuator_B_Contract, LOW);
        digitalWrite(Actuator_C_Extend, LOW);
        digitalWrite(Actuator_C_Contract, HIGH);
        digitalWrite(Actuator_D_Extend, HIGH);
        digitalWrite(Actuator_D_Contract, LOW);
        delay(del);
        digitalWrite(Actuator_A_Extend, HIGH);
        delay((4500)-(del));
        digitalWrite(Actuator_B_Contract, HIGH);
        digitalWrite(Actuator_C_Extend, HIGH);
        digitalWrite(Actuator_D_Contract, HIGH);
        break;

      case '5': //ACTUATOR A Contract
        digitalWrite(Actuator_A_Extend, HIGH);
        digitalWrite(Actuator_A_Contract, LOW);
        break;
        
      case '6': //ACTUATOR A Extend
        digitalWrite(Actuator_A_Extend, LOW);
        digitalWrite(Actuator_A_Contract, HIGH);
        break;

      case '7': //ACTUATOR A+B+C+D Contract
        digitalWrite(Actuator_A_Extend, HIGH);
        digitalWrite(Actuator_A_Contract, LOW);
        digitalWrite(Actuator_B_Extend, HIGH);
        digitalWrite(Actuator_B_Contract, LOW);
        digitalWrite(Actuator_C_Extend, HIGH);
        digitalWrite(Actuator_C_Contract, LOW);
        digitalWrite(Actuator_D_Extend, HIGH);
        digitalWrite(Actuator_D_Contract, LOW);
        delay(4500);
        digitalWrite(Actuator_A_Contract, HIGH);
        digitalWrite(Actuator_B_Contract, HIGH);
        digitalWrite(Actuator_C_Contract, HIGH);
        digitalWrite(Actuator_D_Contract, HIGH);
        break;
        
      case '8': //Actuator Stop on Button Release
        digitalWrite(Actuator_A_Extend, HIGH);
        digitalWrite(Actuator_A_Contract, HIGH);
        digitalWrite(Actuator_B_Extend, HIGH);
        digitalWrite(Actuator_B_Contract, HIGH);
        break;
     
     case '9': //ACTUATOR A and B Extend
        digitalWrite(Actuator_A_Contract, HIGH);
        digitalWrite(Actuator_A_Extend, LOW);
        digitalWrite(Actuator_B_Extend, LOW);
        digitalWrite(Actuator_B_Contract, HIGH);
        break;

      case 'A': //ACTUATOR A and B Contract No Delay
        digitalWrite(Actuator_A_Contract, LOW);
        digitalWrite(Actuator_A_Extend, HIGH);
        digitalWrite(Actuator_B_Extend, HIGH);
        digitalWrite(Actuator_B_Contract, LOW);
        break;

      case 'B': //ACTUATOR A Contract B Extend No Delay
        digitalWrite(Actuator_A_Extend, HIGH);
        digitalWrite(Actuator_A_Contract, LOW);
        digitalWrite(Actuator_B_Extend, LOW);
        digitalWrite(Actuator_B_Contract, HIGH);
        break;

      case 'C': //ACTUATOR A Extend B Contract No Delay
        digitalWrite(Actuator_A_Extend, LOW);
        digitalWrite(Actuator_A_Contract, HIGH);
        digitalWrite(Actuator_B_Extend, HIGH);
        digitalWrite(Actuator_B_Contract, LOW);
        break;

      case 'D': //ACTUATOR A and B Contract
        digitalWrite(Actuator_A_Extend, HIGH);
        digitalWrite(Actuator_A_Contract, LOW);
        digitalWrite(Actuator_B_Extend, HIGH);
        digitalWrite(Actuator_B_Contract, LOW);
        delay(4500);
        digitalWrite(Actuator_A_Contract, HIGH);
        digitalWrite(Actuator_B_Contract, HIGH);
        break;

      case 'E': //ACTUATOR A+B Extend C+D Contract
        digitalWrite(Actuator_A_Extend, LOW);
        digitalWrite(Actuator_A_Contract, HIGH);
        digitalWrite(Actuator_B_Extend, LOW);
        digitalWrite(Actuator_B_Contract, HIGH);
        digitalWrite(Actuator_C_Extend, HIGH);
        digitalWrite(Actuator_C_Contract, LOW);
        digitalWrite(Actuator_D_Extend, HIGH);
        digitalWrite(Actuator_D_Contract, LOW);
        delay(4500);
        digitalWrite(Actuator_A_Extend, HIGH);
        digitalWrite(Actuator_B_Extend, HIGH);
        digitalWrite(Actuator_C_Contract, HIGH);
        digitalWrite(Actuator_D_Contract, HIGH);
        break;

     case 'F': //ACTUATOR A Contract D Extend Delay 4500 B Contract Delay 4500-Del
        digitalWrite(Actuator_A_Extend, HIGH);
        digitalWrite(Actuator_A_Contract, LOW);
        digitalWrite(Actuator_B_Extend, HIGH);
        digitalWrite(Actuator_B_Contract, LOW);
        digitalWrite(Actuator_D_Extend, LOW);
        digitalWrite(Actuator_D_Contract, HIGH);
        delay((4500)-(del));
        digitalWrite(Actuator_B_Contract, HIGH);
        delay(del);
        digitalWrite(Actuator_A_Contract, HIGH);
        digitalWrite(Actuator_D_Extend, HIGH);
        break;

     case 'G': //ACTUATOR B Contract C Extend Delay 4500 A Contract Delay 4500-Del
        digitalWrite(Actuator_A_Extend, HIGH);
        digitalWrite(Actuator_A_Contract, LOW);
        digitalWrite(Actuator_B_Extend, HIGH);
        digitalWrite(Actuator_B_Contract, LOW);
        digitalWrite(Actuator_C_Extend, LOW);
        digitalWrite(Actuator_C_Contract, HIGH);
        delay((4500)-(del));
        digitalWrite(Actuator_A_Contract, HIGH);
        delay(del);
        digitalWrite(Actuator_B_Contract, HIGH);
        digitalWrite(Actuator_C_Extend, HIGH);
        break;

      case 'H': //ACTUATOR E Contract
        digitalWrite(Actuator_E_Extend, HIGH);
        digitalWrite(Actuator_E_Contract, LOW);
        break;
        
      case 'J': //ACTUATOR E Contract
        digitalWrite(Actuator_E_Extend, HIGH);
        digitalWrite(Actuator_E_Contract, HIGH);
        break;
        
      case 'L': //ACTUATOR E Extend
        digitalWrite(Actuator_E_Extend, LOW);
        digitalWrite(Actuator_E_Contract, HIGH);
        break;

        digitalWrite(Actuator_C_Contract, HIGH);
        digitalWrite(Actuator_D_Extend, HIGH);
        digitalWrite(Actuator_D_Contract, HIGH);
        digitalWrite(Actuator_E_Extend, HIGH);
        digitalWrite(Actuator_E_Contract, HIGH);
    }
  }
}

Stefan,

There is a delay in the arduino code. kindly see the code that I shared with xfpd above and let me know your thoughts. Do I need to increase the code or write it a different way? It was working fine previously, so I am not sure why the buttons are behaving like this. If i do a quick press, it acts like it never received the "off" command. It is sent to the hm10, but it does not get processed. A bit above my knowledge level so any input you can provide is much appreciated.

Your code compiles, but I do not see a routine to let the actuators do their work before entering switch(data) to activate another actuator. This typical routine is covered very well in a series of videos covering multitasking.

How did you check the android app that it really did send both commands?
How did you check your hme10-module that your hm10-bluetooth-modul did really receive both commands?

How did you check that

  • the ON-signal
    and
    the OFF-signal

were received in your arduino-code?

best regards Stefan

Stefan,

I asked the programmer to provide a built in serial monitor which shows what commands are being sent to the Hm10 module. So it does show that the commands are being sent. If i do a normal press everything works as it should. It is when the press is very quick that the hm10 module does not recieve the 2nd command (or maybe it is recieved and not processed) not sure. It sounds like xfpd's suggestion with adding a routine to let the actuators do thier work before next command is sent wpuld do the trick. I am trying to watch and understand the videos, but it might be a bit above my knowledge base. Any thoughts on the programming for that?

as a first question:

If you connect your arduino to the computer and you open the serial monitor of the IDE does your code print the received commands?

How should your arduino ever know that your smartphone wants to send another command?
You would have to change your smartphone-app and the arduino-code to have a handshake-mechanism after each command.

If you want more efficient help you should post what your android-app is sending for character-sequences.

best regards Stefan

1 Like

I agree. Disconnect the bluetooth module and use serial monitor to test. It's the only way to know how your sketch behaves.

What are the commands that you send / are supported? In your code there is no such thing as "on" and "off" as far as I can see.

What I can find is single character commands ('1' .. 'L') and Xyyyy. The only reference to "off" is case 8 (stop on button release). If your app did e.g. send '7' followed by '8' you will have to wait 4.5 seconds (worse case) before something happens.

Issue has been resolved. Solved it through modifying the app. According to the programmer, there was an option to send value when previous was successful. Momentary is now working as it should. Thank to everyone for theor input and suggestions. Much appreciated.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.