Switch State Issues?

Hello all my favorite people!

I have a program that is reading the state of a REED switch.
On my Feather M0 it worked flawlessly. Now on ATmega328 (Arduino) its doing the opposite.

It is running the program non stop UNTIL the REED switch gets activated then it stops the program.

It should start running ONLY when the reed switch gets activated.

Hopefully that makes sense?

So i must need to flop some programming or change my setup in some way?

Any help is greatly appreciated!!

//set correct pin numbers as required
#define reedswitch 11
#define vibeMotor 5
#define piezo 9
#define LED 13
long Start = 0;
// this section is Piezo Pitch
#define tonec     3800
#define toned     3900
#define tonee     3950
#define tonep       0

void setup() {
  // put your setup code here, to run once:
  pinMode(reedswitch, INPUT_PULLUP);
  pinMode(vibeMotor, OUTPUT);
  pinMode(piezo, OUTPUT);
  pinMode(LED, OUTPUT);
  digitalWrite(piezo, LOW);
  digitalWrite(vibeMotor, LOW);
  
 
}

void loop() {
  // put your main code here, to run repeatedly:
  begin_:
  if(digitalRead(reedswitch)){//If reed switch active
    Start = millis();
    while((millis()-Start)<6000){
      if(!digitalRead(reedswitch)){
        goto begin_;
        break;
      }
    }
   
    digitalWrite(vibeMotor, HIGH);
    Start = millis();
    while((millis()-Start)<50){
      if(!digitalRead(reedswitch)){
        goto begin_;
        break;
      }
    }
    digitalWrite(LED, HIGH);
    tone(piezo,toned,500);
    Start = millis();
    while((millis()-Start)<2000){
      if(!digitalRead(reedswitch)){
        goto begin_;
        break;
      }
    }
    beep(piezo,tonec,500);
    Start = millis();
    while((millis()-Start)<1000){
      if(!digitalRead(reedswitch)){
        goto begin_;
        break;
      }
    }
    beep(piezo,tonee,500);
    Start = millis();
    while((millis()-Start)<1000){
      if(!digitalRead(reedswitch)){
        goto begin_;
        break;
      }
    }

    beep(piezo,tonee,3000);
    Start = millis();
    while((millis()-Start)<1000){
      if(!digitalRead(reedswitch)){
        goto begin_;
        break;
      }
    }
    //digitalWrite(vibeMotor, LOW);//turn off ibe motor, if you don't need this just comment out
  }
  digitalWrite(vibeMotor, LOW);
  digitalWrite(piezo, LOW);
  digitalWrite(LED, LOW);
}

void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)
{  
int x; 
long delayAmount = (long)(1000000/frequencyInHertz);
long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
for (x=0;x<loopTime;x++) 
{
digitalWrite(speakerPin,HIGH);
delayMicroseconds(delayAmount);
digitalWrite(speakerPin,LOW);
delayMicroseconds(delayAmount);
}
}

As a newbie I am not certain but here goes.

Set the condition on which the code for the reed switch is active.

At the moment they way I read the code is run the script and no state change mentioned?

pinMode(reedswitch, INPUT_PULLUP);

That usually means that the switch is wired from an input to ground and is LOW when the switch is pressed and HIGH when not pressed.

if(digitalRead(reedswitch)){//If reed switch active

This is true when the input is HIGH, false when the input is LOW. If the switch is wired as above the if is true until the switch is pressed.

So how is the switch wired?

The Switch is Wired From Pin 11 to ground

It should start running ONLY when the reed switch gets activated.

The program starts running when the board is powered up or the reset button hit. So there might be the first mistake.

Post a wiring diagram of your setup! Post links to all hardware used which is not an official Arduino board.

if(digitalRead(reedswitch)){//If reed switch active
if(!digitalRead(reedswitch)){// ACTIVE when switch is connected to ground

Note "!"

So this is the program now.
Which is much better.
Now it runs the whole program when the switch is activated. Which is what i want (yay)

But then it just runs the first part for some reason by it self with no switch input??
(this bit) (turns on a LED and Vibe Motor)

digitalWrite(vibeMotor, HIGH);
    Start = millis();
    while((millis()-Start)<50){
      if(digitalRead(reedswitch)){
        goto begin_;
        break;
      }
    }
    digitalWrite(LED, HIGH);
    beep(piezo,tonec,500);
    Start = millis();
    while((millis()-Start)<2000){
      if(digitalRead(reedswitch)){
        goto begin_;
        break;
      }
    }

HEre is the whole program with "!" added and then removed from the other digitalReads\

//set correct pin numbers as required
#define reedswitch 11
#define vibeMotor 5
#define piezo 9
#define LED 13
long Start = 0;
// this section is Piezo Pitch
#define tonec     2500
#define toned     3900
#define tonee     3950
#define tonep       0

void setup() {
  // put your setup code here, to run once:
  pinMode(reedswitch, INPUT_PULLUP);
  pinMode(vibeMotor, OUTPUT);
  pinMode(piezo, OUTPUT);
  pinMode(LED, OUTPUT);
  digitalWrite(piezo, LOW);
  digitalWrite(vibeMotor, LOW);
 
 
}

void loop() {
  // put your main code here, to run repeatedly:
  begin_:
  if(!digitalRead(reedswitch)){//If reed switch active
    Start = millis();
    //digitalWrite(LED2, HIGH); This would be for BALL in Hand LED
    while((millis()-Start)<6000){
      if(digitalRead(reedswitch)){
        goto begin_;
        break;
      }
    }
   
    digitalWrite(vibeMotor, HIGH);
    Start = millis();
    while((millis()-Start)<50){
      if(digitalRead(reedswitch)){
        goto begin_;
        break;
      }
    }
    digitalWrite(LED, HIGH);
    beep(piezo,tonec,500);
    Start = millis();
    while((millis()-Start)<2000){
      if(digitalRead(reedswitch)){
        goto begin_;
        break;
      }
    }
    beep(piezo,toned,500);
    Start = millis();
    while((millis()-Start)<1000){
      if(digitalRead(reedswitch)){
        goto begin_;
        break;
      }
    }
    beep(piezo,tonee,500);
    Start = millis();
    while((millis()-Start)<1000){
      if(digitalRead(reedswitch)){
        goto begin_;
        break;
      }
    }

    beep(piezo,tonee,3000);
    Start = millis();
    while((millis()-Start)<1000){
      if(digitalRead(reedswitch)){
        goto begin_;
        break;
      }
    }
    //digitalWrite(vibeMotor, LOW);//turn off ibe motor, if you don't need this just comment out
  }
  digitalWrite(vibeMotor, LOW);
  digitalWrite(piezo, LOW);
  digitalWrite(LED, LOW);
}

void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)
{ 
int x;
long delayAmount = (long)(1000000/frequencyInHertz);
long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
for (x=0;x<loopTime;x++)
{
digitalWrite(speakerPin,HIGH);
delayMicroseconds(delayAmount);
digitalWrite(speakerPin,LOW);
delayMicroseconds(delayAmount);
}
}

But then it just runs the first part for some reason by it self with no switch input??

It's almost the same as the initial sketch, you just fixed the hardware related issue. You still don't have a program state and react on it.

Maybe by adding comments into your code you could see the faulty logic flow.

Or perhaps missing / misplaced code lines?
Or wrong time checks?

CAPITALS USED FOR EMPHASIS

//set correct pin numbers as required
#define reedswitch 11
#define vibeMotor 5
#define piezo 9
#define LED 13
long Start = 0;
// this section is Piezo Pitch
#define tonec     2500
#define toned     3900
#define tonee     3950
#define tonep       0

void setup() {
  // put your setup code here, to run once:
  pinMode(reedswitch, INPUT_PULLUP);
  pinMode(vibeMotor, OUTPUT);
  pinMode(piezo, OUTPUT);
  pinMode(LED, OUTPUT);
  digitalWrite(piezo, LOW);              INITIALIZE / DISABLE PIEZO 
  digitalWrite(vibeMotor, LOW);        INITIALIZE / STOP MOTOR 
 
 
}

void loop() {
  // put your main code here, to run repeatedly:
  begin_:
  if(!digitalRead(reedswitch)){//If reed switch active
    Start = millis();                            GET STARTUP TIME 
    //digitalWrite(LED2, HIGH); This would be for BALL in Hand LED
    while((millis()-Start)<6000){         IS RUNNING TIME < 6  SECONDS     
      if(digitalRead(reedswitch)){        RUNNING TIME < 6 SECONDS , CHECK THE SWITCH 
        goto begin_;                            SWITCH IS OFF, START OVER 
        break;                                     huh? HOW DO YOU  GET HERE ? 
      }
    }                                                 
   
    digitalWrite(vibeMotor, HIGH);
    Start = millis();
    while((millis()-Start)<50){
      if(digitalRead(reedswitch)){
        goto begin_;
        break;
      }
    }
    digitalWrite(LED, HIGH);
    beep(piezo,tonec,500);
    Start = millis();
    while((millis()-Start)<2000){
      if(digitalRead(reedswitch)){
        goto begin_;
        break;
      }
    }
    beep(piezo,toned,500);
    Start = millis();
    while((millis()-Start)<1000){
      if(digitalRead(reedswitch)){
        goto begin_;
        break;
      }
    }
    beep(piezo,tonee,500);
    Start = millis();
    while((millis()-Start)<1000){
      if(digitalRead(reedswitch)){
        goto begin_;
        break;
      }
    }

    beep(piezo,tonee,3000);
    Start = millis();
    while((millis()-Start)<1000){
      if(digitalRead(reedswitch)){
        goto begin_;
        break;
      }
    }
    //digitalWrite(vibeMotor, LOW);//turn off ibe motor, if you don't need this just comment out
  }
  digitalWrite(vibeMotor, LOW);
  digitalWrite(piezo, LOW);
  digitalWrite(LED, LOW);
}

void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)
{
int x;
long delayAmount = (long)(1000000/frequencyInHertz);
long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
for (x=0;x<loopTime;x++)
{
digitalWrite(speakerPin,HIGH);
delayMicroseconds(delayAmount);
digitalWrite(speakerPin,LOW);
delayMicroseconds(delayAmount);
}
}

Moderator edit: CODE TAGS ADDED

Pylon

What do you mean by program state? and where would that be added.

Sorry i'm still new to this.

Thanks all!

What do you mean by program state? and where would that be added.

A variable in your program that holds the current state. It might be a simple byte variable where 0 is the startup value, 1 is motor running, 2 is motor stopped and LED on, etc.

Then you simply react on that state in your main loop and remove all while loops inside it. You simply check the state variable and depending on it's value you can set outputs, react differently if the reed switch is triggered, etc.