Switch State?

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
StrikeforceInd
 
Posts: 16
Joined: Wed Jul 26, 2017 11:24 am

Switch State?

Post by StrikeforceInd »

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 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!!

Code: Select all

//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);
}
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Switch State?

Post by adafruit_support_bill »

You have the internal pullup resistors enabled:

Code: Select all

  pinMode(reedswitch, INPUT_PULLUP);
So the default state when the switch is open will be logic HIGH.
Assuming that you have it connected between the reedswitch pin and GND, it will go LOW when the switch is closed.

logic HIGH will be interpreted as TRUE in an 'if' statement like this:

Code: Select all

if(digitalRead(reedswitch)){//If reed switch active
So it will assume the switch is 'active' when it is open.

If you want to invert that logic, just use the NOT operator: '!' like this:

Code: Select all

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

User avatar
StrikeforceInd
 
Posts: 16
Joined: Wed Jul 26, 2017 11:24 am

Re: Switch State?

Post by StrikeforceInd »

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)


new code

Code: Select all

//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);
}
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Switch State?

Post by adafruit_support_bill »

How do you have the switch wired?

User avatar
StrikeforceInd
 
Posts: 16
Joined: Wed Jul 26, 2017 11:24 am

Re: Switch State?

Post by StrikeforceInd »

The switch is Wired from Pin 11 to the Ground

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Switch State?

Post by adafruit_support_bill »

Is it a 'normally open' or 'normally closed' switch?
If you have a link to the switch specifications, please post it here.

User avatar
StrikeforceInd
 
Posts: 16
Joined: Wed Jul 26, 2017 11:24 am

Re: Switch State?

Post by StrikeforceInd »

it is a normally open reed switch

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Switch State?

Post by adafruit_support_bill »

Not sure what would make it do that.

You might try changing your 'if' statement to this form which is technically more correct:

Code: Select all

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

User avatar
StrikeforceInd
 
Posts: 16
Joined: Wed Jul 26, 2017 11:24 am

Re: Switch State?

Post by StrikeforceInd »

Do that to all of the IF statements?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Switch State?

Post by adafruit_support_bill »

Yes. Typically LOW is equivalent to 0 or FALSE and HIGH is equivalent to 1 or TRUE. But that is not guaranteed to be the case.

So just to be sure, testing for "digitalRead(reedswitch) == LOW" will remove all doubt.

User avatar
StrikeforceInd
 
Posts: 16
Joined: Wed Jul 26, 2017 11:24 am

Re: Switch State?

Post by StrikeforceInd »

This program Works.
BUT It runs the top 3 lines by itself ( "wait 6000", "vibemotor" and "LED" lines) after the switch is deactivated...
Almost like its not checking the switch state or resetting and waiting again?

Is there a code error?

Thanks so much!

Code: Select all

//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     2000
#define toned     3900
#define tonee     3960
#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) == LOW){//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);  //ITS RUNNING THIS 
    Start = millis();
    while((millis()-Start)<50){
      if(digitalRead(reedswitch)){
        goto begin_;
        break;
      }
    }
    digitalWrite(LED, HIGH);  //AND RUNNING THIS ONCE AFTER THE CODE RUNS ONCE
    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);
  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);
}
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Switch State?

Post by adafruit_support_bill »

Add some serial output to your code so that you can follow the actual path of execution in the serial monitor.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”