I'm working on a Hair brush project based on ATmega328. It is supposed to have a button which when pressed once will turn on the hair brush and set it to the least vibration mode, when the same key is pressed again the hair brush will be set on medium vibrating mode and when the same key is pressed thrice it will be set to highest vibrating mode. But if I long press and hold this button the hair brush should turn OFF. Something like cellphone. The hair brush is battery operated.
How this can be achieved? I have no idea where to start. I can handle the vibration mode stuff but this ON and delayed OFF thing, how to do it?
Look for button libraries; there are several that allow you to read a short vs. a long press. Combine this with a finite state machine and you've got the basics. Then implement in such a way that you don't use delay() as it will mess up responsiveness of the device to user input.
As to getting started: I'd say start by reading a button and distinguishing between long and short press; once you got that down, experiment with making a simple state machine that cycles through a couple of predefined states. Then combine the button and the state machine. Finally write the code for the relevant outputs (leds, motors etc) for each particular state.
Thank you for all of these wonderful and helpful inputs I'll try to implement them. But how do I disconnect the power and turn on the power to my hair brush with same switch/button? what type of hardware changes I need to make?
The power to the motor is not directly controlled by the button. Only a tiny signal comes from the button, telling the chip if the button is pressed or not. The logic in the program running on the chip controls the motor, that logic changes in response to the signal from the button.
As mentioned above, the chip cannot control the motor directly. A driver circuit/module is required between the atmega chip and the motor because the motor will require more power than the atmega's pins can provide.
if you are asking about a hardware issues, wiring, circuits, what chips, motor drivers, etc, you need to work up the bits needed.
microcontrollers use micro-electrics.
a switch says, I am open or I am closed
the micro-controller says, if the start switch is closed, then send a signal to the motor driver to run the motor.
the motor driver sees this low power signal and it allows high power to be sent to the motor.
an obvious solution which no-one has mentioned yet.
watch some videos on Arduino pump or Arduino fan or Arduino motor
take some time and follow the many tutorials on how to do the example sketches that are in the IDE. as you learn the basics, you will gain the knowledge to make this sort of project.
The problem you have selected to challenge is just a bit more complicated.
Cell phones are never really fully off. At least they are able to see and respond to an on/off button, and more than turn on, they wake up.
So in addition to hacking the idea of first, second and thrice button presses, and short and long, you'll have to manage the power with some extra ordinary circuitry and processor states.
I suggest you get a prototype working that just has an on/off switch, and a pushbutton for speed control one-two-three.
Then add long presses that simply light an LED so you know you are programmed correctly to see the long press, even if it doesn't turn off you device just yet.
Then look into low power and sleep modes, and how to get an Arduino to sleep soundly whilst waiting for that wakeup call.
Don't be intimidated, low power is fairly straight ahead if not a place for a total noob to start.
Read the link below as if you understood what he's talking about! It is a slog, but he has complete workable examples and good tips for how to fix up the hardware for low power:
Seatch for arduino turn itself off with transistor.
You can have power to the arduino controled by a transistor.
The arduino itself controls the transistor.
You press and hold a button.
The button temporarily powers the transistor.
The arduino turns on
The arduino then maintains power to the transistor and from that point is in control
Later you press and hold the power button.
The arduino sees that and removes power from itself
first fish dragged out of the google pond has a nifty circuit that will work well, be sure to see the one where button status can be seen whilst the Arduino is powered.
Thank you so much Alto for breaking up the things simpler for me. I'm proceeding to implement that circuit already.
All the MOSFETS that I found were too big in terms of size and specs as well. So I decided to use SMD P Channel FET and SMD N channel FET. Would it make any difference? if I use FETs instead of MOSFETS?
I just have to give the schematics for this hair brush and the coding file so I won't be testing them. So, I guess this will be solution for my case.
Thank you so much all of you for taking time and giving all those timely and valuable inputs.
I can’t say about MOSFET vs. FET. Without going beyond my zone.
Surface mount devices are fine. If you are driving the hair brush vibrator (?) with the same FET, obvsly it will have to handle the max current of that in addition to the small current the control unit will need.
I am sure you do mean to test the software, somehow. I don’t think I’ve ever written something that didn’t need some fixing when it was entered and tested.
So you will need to fake the power on power off part.
Power on: that’s just reset or turn on the control unit with a switch. That’s what the program would experience with the real circuit attached.
Power off: to recognize the power off long push and prove that you have done, just use an LED and light it up to show when you circuit, if it was there, would turn things off.
I can’t leave without mentioning wokwi.com, my new favorite toy, which makes all kindsa testing much less painful than the normal cycle of development using a real Arduino.