0% found this document useful (0 votes)
11 views3 pages

To Create An Ar

The document provides an Arduino program that controls a motor based on button presses, allowing up to 10 presses. It includes pin definitions, setup, and loop functions for reading button states, incrementing a press counter, and operating the motor. Additionally, it outlines wiring considerations for connecting the button and motor control components.

Uploaded by

Ifaa Biddiiqaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

To Create An Ar

The document provides an Arduino program that controls a motor based on button presses, allowing up to 10 presses. It includes pin definitions, setup, and loop functions for reading button states, incrementing a press counter, and operating the motor. Additionally, it outlines wiring considerations for connecting the button and motor control components.

Uploaded by

Ifaa Biddiiqaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

To create an Arduino program that operates a motor based on a button press (up to 10 times), you can

use the following sample code. This code assumes you have a motor controlled using a relay module or
an H-bridge, and a button connected to a digital pin.

### Arduino Code

```cpp

// Pin definitions

const int buttonPin = 2; // Pin for the button

const int motorPin = 9; // Pin for the motor control

int buttonState = 0; // Variable for reading the button status

int pressCount = 0; // Count of button presses

void setup() {

pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with pull-up resistor

pinMode(motorPin, OUTPUT); // Set motor pin as output

Serial.begin(9600); // Start serial communication for debugging

void loop() {

buttonState = digitalRead(buttonPin); // Read button state

// Check if button is pressed (LOW due to pull-up)

if (buttonState == LOW) {

delay(50); // Debounce delay

if (digitalRead(buttonPin) == LOW) { // Check if still pressed


pressCount++; // Increment counter

Serial.print("Button pressed: ");

Serial.println(pressCount);

// Run the motor if button pressed less than 10 times

if (pressCount <= 10) {

digitalWrite(motorPin, HIGH); // Turn motor ON

delay(1000); // Run motor for 1 second

digitalWrite(motorPin, LOW); // Turn motor OFF

delay(100); // Delay to prevent multiple counts in a single press

// If pressed 10 times, reset counter

if (pressCount >= 10) {

pressCount = 0; // Reset count after 10 presses

Serial.println("Counter reset.");

```

### Explanation:

- **Pin Definitions**: The `buttonPin` is set to 2 (you can change this according to your wiring). The
`motorPin` is set to 9.
- **Setup Function**: Initializes the button pin as an input with an internal pull-up resistor and the
motor pin as an output.

- **Loop Function**:

- Reads the button state.

- If pressed, waits briefly to debounce.

- Increments the `pressCount` and prints it to the serial monitor.

- If the button has been pressed 10 times, the counter resets.

- The motor turns on for 1 second each time the button is pressed until the count reaches 10.

### Wiring Considerations:

- Connect the button between the button pin and ground.

- Connect the motor control pin to the motor driver or relay.

- Make sure to provide adequate power supply for the motor as required based on your setup.

You might also like