How to add a power button to control entire arduino system?

I want to create a useless box that has 5 toggle switches and a corresponding arm for each switch. I also want to create a master on/off switch for the entire circuit so that I can control when it is active or not. I've attached my switch to the vin and also the positive side of my 9v power supply. The other side of the power supply is attached to the negative side of the battery.

The way I attempted to do this was by checking the status of the switch at the top of my loop, but I'm struggling to declare the switch since it's only attached to the vin pin of my arduino. What is a better way to do this? I'm new to using arduinos and can't seem to find any solutions online, so I apologize if this is a dumb question

#include <Servo.h>

Servo doorServo;
Servo fingerServo1;
Servo fingerServo2;
Servo fingerServo3;
Servo fingerServo4;
Servo fingerServo5;

int onOff = 5;
int swPin1 = 0;                    
int swPin2 = 1;
int swPin3 = 2;
int swPin4 = 3;
int swPin5 = 4;
int pos = 0;                      
int selectedMove = 0;
int selectedMove2 = 0;
int selectedMove3 = 0;
int selectedMove4 = 0;
int selectedMove5 = 0;

void setup() {
  pinMode(swPin1, INPUT_PULLUP);          
  pinMode(swPin2, INPUT_PULLUP);  
  pinMode(swPin3, INPUT_PULLUP);
  pinMode(swPin4, INPUT_PULLUP);
  pinMode(swPin5, INPUT_PULLUP);
  doorServo.attach(8);            
  doorServo.write(0); 
  fingerServo1.attach(9);          
  fingerServo1.write(80);
  fingerServo2.attach(10);
  fingerServo2.write(80);
  fingerServo3.attach(11);
  fingerServo3.write(80);
  fingerServo4.attach(12);
  fingerServo4.write(80);
  fingerServo5.attach(13);
  fingerServo5.write(80);
}

void loop() {
  if (digitalRead(onOff) == LOW){
  if (digitalRead(swPin1) == LOW) {   //switch pin 1
    if (selectedMove > 2) {      
      selectedMove = 0;           
    }
    if (selectedMove == 0) {     
    for (pos = 0; pos < 140; pos += 3) {  
      doorServo.write(pos);               
      delay(20);                          
    }
    for (pos = 0; pos < 200; pos += 4) {  
      fingerServo1.write(pos);            
      delay(10);                         
    }
    for (pos = 200; pos >= 0; pos -= 4) { 
      fingerServo1.write(pos);             
      delay(20);                          
    }
    for (pos = 140; pos >= 0; pos -= 3) { 
      doorServo.write(pos);               
      delay(10);                          
  }    
    }else if (selectedMove == 1) {  //move 2
    }
    selectedMove += 1;
  }

When your switch is closed, the system has power and functions.

When it is open, there is no power, the system is off, no function.

If you could somehow check the status of the power switch, you would necessarily find it to be closed, otherwise how would the system be functioning?

Perhaps an idea like using a servo to physically move the switch to the off position could be worked out.

Or, the physical switch could be replaced by a logically controlled power transistor.

Here's an article that gets into it a bit. Others have their favorite versions of this commonly used technique.

HTH

a7

That's the easiest way to do it.
May I ask how the project is performing

1 Like

all the other aspects of it are working, however I don't know how to declare the switch since its attached to the vin pin. How do I fix this?

You don't need any special detection. If the code is running, the switch is closed. Any detecting circuitry and code will always tell: "Closed".
Please read reply #2 again.

Welcome to the forum

How will the Arduino that you have turned off read the state of a pin ?

Why?
If the switch is OFF, the Arduino is not powered up.
If the switch is ON, the Arduino is powered up.

You do not need to sense if there is volts or not.

I think you are over thinking your simple project.

Have you actually built this project and tested what it does?

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

OK, so what if you were told that the magic number for the Vin pin was 742 and you could

int vinSwPin = 742;

like you did the 5 other switches?

Then what? How is it that yoiu want to or hope to exploit this now-declared pin?

a7

One approach is the Pololu Power Switch. Push a button to turn it on or off, and the Arduino can turn it off under program control.

Obviously, a turned-off Arduino cannot turn itself back on.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.