Switch Flicking Through Magnet and Webhook

by justaca in Circuits > Microcontrollers

53 Views, 0 Favorites, 0 Comments

Switch Flicking Through Magnet and Webhook

20231106_020255.jpg

This is a mechanism which turns a motor up and down through a webhook sent online and/or through magnet detection. The two work together in tandem so as neither get in the way of each other. (Although my example ended up not working, it is applicable to more permanent and better thought out solutions)

Supplies

  • Breadboard
  • Stepper motor
  • Hall effect sensor
  • Magnet
  • Wifi sensor
  • Power source
  • A number of wires

Note that I used a Particle Argon for this project.

The actual switch flicking through the motor can be done in a few ways. For me, all I needed was a demonstration, so the plan was to let a string be pulled by the stepper motor to switch on the lights, and simply twist the motor back to let the switch turn back by itself. However, this didn't work in the end. This might work on switches which require less force to flick, though. If not, then you could try putting the switch between two sticks and then turning the motor to flick the switches. As long as turning the motor will flick the switch, it should work fine.

The Physical Components

Start by making connecting each part to the breadboard together. This is relatively simple--connect the hall effect sensor and stepper motor power to the breadboard, give them power (it might differ depending on model), connect them to ground, and also make sure you have a noticable way to detect if the hall effect sensor detects a magnet. I had an LED usually lit and then had it turn off whenever it detected a magnet.

The Code and Webhook

Screenshot 2023-11-12 11.14.38 PM.png
Screenshot 2023-11-12 11.15.25 PM.png
Screenshot 2023-11-12 11.15.47 PM.png

Once everything is set up physically, the hard part of this project was the code. Making the magnet and webhook work together caused many problems. Note that my public function was named to fit my needs, and if you want your webhook to work, you need to make sure your code and webhook have the same name listed.

IMPORTANT: The int variable "track" behaves like a boolean throughout this code.

My entire code (in text) is listed here:

#include <Stepper.h> // So the stepper motor works
int stepsPerRevolution = 2048; // Change this number to change how much the motor turns

// Change the pins below to match your Argon set up
#define IN1 2
#define IN2 3
#define IN3 4
#define IN4 5

Stepper myStepper(stepsPerRevolution, IN1, IN3 , IN2, IN4);

int pinHall = 7; // Pin for the Hall sensor -- change as needed
int track; // To track the stepper motor turning -- works much like a boolean
bool previousState = false;
bool currentState = true;
bool changedState = false;

void setup() {
  myStepper.setSpeed(10); // turn speed -- change as needed
  
  pinMode(pinHall, INPUT); // sets the digital pin as input
   
  Particle.function("turn", turnFunc); // for the webhook--change as needed
}

void loop() {
   
  currentState = (digitalRead(pinHall) == LOW);
  if (currentState != previousState){
    changedState = true;}
  else{
    changedState = false;}
  previousState = currentState;
   
  if(changedState){
    if(digitalRead(pinHall) == HIGH){
      track = 0;
    }
   
    else if(digitalRead(pinHall) == LOW){
      track = 1;
    }
     
    if(track == 0){
      digitalWrite(pinHall, HIGH);
      myStepper.step(stepsPerRevolution);
      track = 1;
    }
     
    else if(track == 1){
      digitalWrite(pinHall, LOW);
      myStepper.step(-stepsPerRevolution);
      track = 0;
    }
  }
}

// Everything is above is for the hall effect sensor
// Everything below is for the webhook

 int turnFunc(String param){ // remember, change as needed
    
    if (track == 1){
      if (digitalRead(pinHall == LOW)){
        myStepper.step(stepsPerRevolution);
      }
      else {
        myStepper.step(-stepsPerRevolution);
      }
    }
    else {
      if (digitalRead(pinHall == LOW)){
        myStepper.step(-stepsPerRevolution);
      }
      else {
        myStepper.step(stepsPerRevolution);
      }
    }
    stepsPerRevolution = stepsPerRevolution * -1;
    
  return 0;
}

Webhook Activation

Screenshot 2023-11-12 11.13.49 PM.png
Screenshot 2023-11-12 11.19.25 PM.png

For me, I simply used a website to fulfill this, although there are likely other ways to do it. I used https://reqbin.com/ which requires specific information about your microcontroller, such as its device ID and access token. Remember that the webhook needs the public function name, so you must make sure that the webhook and code have the same name for the webhook.

Setup

20231106_020240.jpg

My example has a magnet everything taped onto and/or near a door (note that it might've not been necessary to tape everything up if longer wires were used), while the hall effect sensor is right next to where the magnet will end up while the door is closed. Supposedly, closing the door would make the hall effect sensor detect the magnet and flick the switch downwards and the opening the door would flick the switch upward. The webhook would flick the switch, even if the door was closed or opened (magnet detected or not). If everything is working correctly, these two methods of flicking the switch will work perfectly in tandem with each other.