Arduino Programming for Thor's Hammer Question

Hello Everyone,

I am making a Thor's hammer replica and i have a question. I have an electromagnet in the hammer, an ultrasonic sensor, and a magnetic switch (the kind used on doors for security systems). The electromagnet is run through a normally-open relay. The ultrasonic sensor is at the base of the handle, and the magnetic switch is in the handle. I am trying to code my Arduino Uno to close the relay every time a person grips the handle (by reading the distance from the ultrasonic sensor). However, when I use a "worthiness" glove with an embedded magnet to activate the magnetic switch in the handle, i want the Arduino to override the ultrasonic sensor and leave the relay open. Any help is greatly appreciated.

What have you tried ?

I've modified an ultrasonic distance detector code to switch the relay instead of powering LEDs, but I can't override the code when I activate the magnetic switch.

but I can't override the code when I activate the magnetic switch.

Sure you can. You have our permission.

That you don't is a completely different story. Now, go read the stickies at the top of the forum - the ones you were supposed to read BEFORE you posted, and figure out what you left out. Like, ALL OF YOUR CODE!

Ok. I did a bit of Googling and I made this code which works. I dumped the ultrasonic sensor for a PIR sensor. Could you guys let me know if there is a better way of programming it, especially the LED blink part at the end? The code looks a little messy to me.

int ledPin = 13;                //pin for the LED
int inputPin = 3;               //input pin (for PIR sensor)
int pirState = LOW;             //
int val = 0;
int relayPin = 8;

const int buttonPin = 8; 
int buttonState = 0; 

 
void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(buttonPin, INPUT);    // declare button as input
  pinMode(relayPin, OUTPUT);    // declare relay as output
}
 
void loop(){
  buttonState = digitalRead(buttonPin);
  
  val = digitalRead(inputPin); 
  if (val == HIGH) 
  {
    digitalWrite(ledPin, HIGH);
    digitalWrite(relayPin, HIGH);
    if (pirState == LOW) {
      pirState = HIGH;
    }
  } 
  
  else if (buttonState == LOW)
  {
      digitalWrite(ledPin, LOW);
      digitalWrite(relayPin, LOW);
    }

    else
    {
    if (pirState == HIGH){
      pirState = LOW;
      digitalWrite(relayPin, LOW);
      digitalWrite(ledPin, LOW); // this is to make the led blink to indicate a wait time
      delay(500);
      digitalWrite(ledPin, HIGH);// since my pir sensor takes about 3 seconds to reset
      delay(500);
      digitalWrite(ledPin, LOW);
      delay(200);
      digitalWrite(ledPin, HIGH);
      delay(500);
      digitalWrite(ledPin, LOW);
      delay(200); 
      digitalWrite(ledPin, HIGH);
      delay(500);
      digitalWrite(ledPin, LOW);
      delay(200);
      digitalWrite(ledPin, HIGH);
      delay(500);
      digitalWrite(ledPin, LOW);
      delay(200);
      digitalWrite(ledPin, HIGH);
      delay(500);
      digitalWrite(ledPin, LOW);
      delay(200);
      digitalWrite(ledPin, HIGH);
      delay(500);
      digitalWrite(ledPin, LOW);
      delay(500);
    }
    }
  }

If this helps, here is some more info:
Sketch uses 1,626 bytes (5%) of program storage space. Maximum is 32,256 bytes.
Global variables use 21 bytes (1%) of dynamic memory, leaving 2,027 bytes for local variables. Maximum is 2,048 bytes.

Thanks for the help.

P.S. Sorry for not posting the code, PaulS
P.P.S. My blinks do have delays, UKHeliBob. It was simpler. :frowning:

If you insist on using delay() to flash the LED then at least put it in a for loop

      for (int flash = 0; flash < 7; flash++)
      {
        digitalWrite(ledPin, LOW);
        delay(200);
        digitalWrite(ledPin, HIGH);
        delay(500);
      }

Thanks, UKHeliBob. I'll add that to my code. By the way, is there another way to program a blink without using delays? i saw one that uses a variable, but that seemed like overkill for a simple program like mine.

is there another way to program a blink without using delays?

You could always try looking at the BlinkWithoutDelay example....

The technique is used when you must keep loop() going without blocking such as when you want you blink an LED and read inputs at what appears to be the same time. Also have a look at Several things at the same time

Wow. That code in that post is incredible! I think I'll use that in future projects. I'll also look at the example. Thanks a lot, UKHeliBob.