I am using an Maker Uno Plus (Arduino Educational Board) which I find inspiring and have made a Watch Winder for my automatic Rolex when I'm working in the shed, garden and off at night times etc. The winder works perfectly which I programmed following the many links on YouTube and Kit suppliers. However, I am supplying the board via the onboard micro USB which is plugged into a wall adaptor located underneath my desk.
The controller is fed from the 5V USB and the 5V stepper motor driver has a 5V onboard supply and ground linked to the driver which the motor is plugged into.
My question is: can I hard (or soft) wire an on/off switch at the watch winder considering the impracticalities of breaking into the USB supply cable which I want to keep complete for maybe later programming updates etc.
I am a retired electrical engineer and a beginner at actual programming but find the Arduino micro controller an interesting new hobby for me.
Welcome to the forum
It sounds like you can't easily reach the adapter to turn it off or unplug it and don't want to just pull out the USB connector from the Arduino. If so, consider connecting a switch to an Arduino pin, sensing its state in the code and depending on that state run or don't run portions of your code
You can buy micro USB inline switches which plug into your existing micro USB power lead and on to your Arduino. The PiHut has one, and there are probably many on ebay etc.
Thanks UKHeliBob - yes it's not easily reached. I have seen mention of pullup/pulldown switches but as a beginner would need help identifying the pin and which type of switch - momentary or simple toggle. Also the programme is on a loop so I suppose it would finish the sketch and continue until the button or switch is operated. Would I add an extra line of code at the end of the existing program?
Thanks markd833 I will definitely get one of these inline switches and will also carry on with the software suggestion as well (as a learning stage) which will, I'm sure, be useful in future projects.
I've just a few minutes ago completed an order for another Maker Uno Plus board with the PiHut.
What you do is to have the code read the switch state frequently. Each time through loop() is ideal.
When you detect that the switch is closed you set a boolean variable to true rather than false and vice versa. Then, in the sketch, you control which portions of the sketch you want to run when the boolean is true or false.
If you want you can prevent anything running apart from the switch detection code. You may also want to make the system take up a standby position while the switch is closed
Thanks, I appreciate your reply but as I said I'm a beginner in this field and I am actually a bit out of my depth with your reply which is very much appreciated.
I find it strange that it seems so complicated to switch off a device with having to using boolean variables , and true or false statements and switch detection codes.
I will have to study a bit more before I can understand these commands that I wanted to implement.
Much appreciated for your comments.
If you are happy to use a physical switch to turn off the power to the Arduino then go with that
Control using a button does not have to be complicated
#include "Button2.h"
Button2 button;
const byte BUTTON_PIN = A3;
boolean running = true;
void setup()
{
Serial.begin(115200);
button.begin(BUTTON_PIN);
button.setClickHandler(click);
}
void loop()
{
button.loop();
if (running)
{
// YOUR CODE GOES HERE
Serial.println("Click the button to stop your code running");
}
}
void click(Button2& btn)
{
running = !running;
if (!running)
{
Serial.println("Click the button to restart your code");
}
}
Thanks madmark2150 for the light hearted response.
Actually it is becoming a global issue regarding the billions of devices sitting all day in standby mode. I can't remember when I last had a TV with a proper off switch - how many power stations are running world wide just to supply the standby current?
Thanks for the response UKHeliBob, I will definitely work with your code which is much appreciated. It has inspired a project I will look into regarding remote controlled devices.
I am sure that I can find instructions for this IR control mode.
Just to clarify something Bob, BUTTON_PIN is identified as A3 - easy but is the physical button normally open or closed (NO/NC) as I have been accustomed to identify switch and relay contacts? I suppose it is connected between ground and pin A3?
As written the switch contacts would be NO and take the pin LOW when closed but the code could be changed to use NC contacts if it were necessary
Great, thanks very much.
Thanks UKHeliBob,
I have pasted my proven code into your solution as instructed to 'insert here' but when compiled I kept getting error messages which I tried to correct only to receive another different error message. I have saved a few but have now decided to learn Arduino programming from scratch and hopefully be able to resolve these 'error' issues myself.
In the meantime I am using the inline ON/OFF USB switch as suggested by markd833 which works fine for now. I use my watch winder regularly but intend to solve the 'soft switch' solution when I've become more competent.
As I said before I've took up this as a hobby in my retirement to keep my brain active as now 76 years.
Thanks all for your responses which are definitely appreciated and I'm sure I will be communicating with this forum in the future.
We cannot see your sketch or the error messages. Please post both of them, using code tags for both
The thing with arduino is that you need to understand a few fundamental concepts. Once you do it gets a lot easier. The most important are:
- loop
- millis timing
- state machines
The loop function is not immediately intuitive for humans. Basically it does what it says on the tin and loops very fast. We tend to think in straight lines, one thing after the other, so it confuses us. In loop your code runs again and again from top to bottom and so you need to remember that something was done last time through loop and now you want something else to be done otherwise it will just do it all. This is where the Boolean true false things come in. (Another confusing thing that is just the simplest 2 state variable 1/0, on/off true/false). Look at this pseudocode example:
Loop(){
Do something
Do something else
}
This will do something then do something else then do something then do something else…and on and on
You only want to do something else if a button is pushed:
Loop(){
Do something
Button poll
If button push detected do something else
}
This will do something, check buttons, if button pushed do something else, do something, check buttons… over and over again. If the button is not pushed it will just do something and check the buttons over and over again
In general we don’t want that stuff to happen over and over every millisecond so we build a state machine and add some timing
Loop(){
State is 0 (or false)
If state is 0 Do something and Button poll
If button becomes pressed change state to 1
If state has changed do something else until a time has elapsed then state is 0 again
}
In the last example we have 2 states 0 and 1. They can be true and false if you prefer. The code runs many times a second and checks the state and only runs the associated code IF that state is true. Therefore we can split our code. We can have as many states as we like with different code running in each. We must remember that we need to have something to change the state such as a buttonPush and we need something to change the state back or increment to the next state.
This is very crude pseudocode to get the concept across. You should study:
Blink without delay example
State machines
Thanks UK HeliBob,
However I've now purchased a tutorial book by Simon on the programming of the Arduino so will wait until I've finished all the lessons before I embark with this soft stop switch as the hard wired USB switch is working great with the very useful watch winder.
Thanks pmagowan for your knowledgeable input and I will keep it saved for when I am up to speed with the programming from basics.
May I say that I am searching the net for Arduino based data logging for a practical solution to recording and printing the run details of my effluent pump motor.
This data will be taken from an auxiliary relay hard wired in parallel to the main motor contactor in the local control panel (easy bit) and hard wired to an Arduino data logger and shield module.
However, all the circuits and help up to now that I've seen are for analogue sensors mainly temperature, pressure and humidity. I would think that the ON/OFF circuit I require is possibly even more basic but I will have to keep searching. This is a practical and useful project that I would love to complete like the watch winder.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
