millis() Endless Loop

I'm fairly familiar with Arduino programming and have used debouncing and millis a bitt but I'm stumped on this. This code is for a project with a vibration sensor or shake switch. It's pretty twitchy and needs about 800-1000 millis to reset. I'm fine with waiting but for some reason, if I hit the switch just right (it's not that hard to do) it will get into a loop with this code and keep running the button read function. I inevitably have to hit the switch again to get out of it. Is there a basic problem with this logic here?

This is a stripped down version of the code. The idea is that I bump the vibration switch and it sends midi over wifi using the AppleMidi library in the full program. If we can achieve that with a different approach, I'm all ears.

Thanks for any help in advance.

#define       switchPin    4     
int greenPin = 2;
int period = 1000;
unsigned long currentMillis=0;
unsigned long previousMillis=0;
  
void setup()
{
    pinMode(switchPin, INPUT_PULLUP);
    digitalWrite(switchPin, HIGH);
    pinMode(greenPin, OUTPUT);
    // Serial communications and wait for port to open:
    Serial.begin(115200);
}

void loop ()
{
  currentMillis = millis();
  ReadButton();
}

void ReadButton()
{
  if (digitalRead (switchPin) == LOW)
  {
    if ((unsigned long)(currentMillis - previousMillis) > period){
      Serial.println("switch on");
      Serial.println("switch off");
      previousMillis = millis();
      }
  }
}

hongkongstrong:
I’m fairly familiar with Arduino programming and have used debouncing and millis a bitt but I’m stumped on this. This code is for a project with a vibration sensor or shake switch. It’s pretty twitchy and needs about 800-1000 millis to reset. I’m fine with waiting but for some reason, if I hit the switch just right (it’s not that hard to do) it will get into a loop with this code and keep running the button read function. I inevitably have to hit the switch again to get out of it. Is there a basic problem with this logic here?

This is a stripped down version of the code. The idea is that I bump the vibration switch and it sends midi over wifi using the AppleMidi library in the full program. If we can achieve that with a different approach, I’m all ears.

Thanks for any help in advance.

#define       switchPin    4     

int greenPin = 2;
int period = 1000;
unsigned long currentMillis=0;
unsigned long previousMillis=0;
 
void setup()
{
    pinMode(switchPin, INPUT_PULLUP);
    digitalWrite(switchPin, HIGH);
    pinMode(greenPin, OUTPUT);
    // Serial communications and wait for port to open:
    Serial.begin(115200);
}

void loop ()
{
  currentMillis = millis();
  ReadButton();
}

void ReadButton()
{
  if (digitalRead (switchPin) == LOW)
  {
    if ((unsigned long)(currentMillis - previousMillis) > period){
      Serial.println(“switch on”);
      Serial.println(“switch off”);
      previousMillis = millis();
      }
  }
}

How can you debug your program when you print it is on, then print it is off?

if ((unsigned long)(currentMillis - previousMillis) > period){
Serial.println(“switch on”);
Serial.println(“switch off”);
previousMillis = millis();

Paul

Your calling the function Readbutton(), but you said you don’t want to read the button unless it’s 1000 millis since the code executed prior.. But your function reads the button then checks for time..

Technically it works, but based on that logic, it would be easy to believe your problem is in all the other code.

Plus, are you sure your button isn’t just “sticking”.. you said you have to press it a certain way and then it’s reset when you push it again.. that sounds more like a mechanical failure than code.

Thanks for the replies! The whole code includes a midi on and midi off message (which is important that they both happen so you avoid stuck midi notes) but I didn't really clean that up for this example.

I think the hardware must be the issue. These are little vibration switches and one of the wires is super thin. That makes most sense.