Alex Cockman
Published

Arduino Project 1

Using buttons and potentiometer to run Arduino 30/90 degrees with buttons and 180 degrees with potentiometer.

BeginnerProtip3 hours196
Arduino Project 1

Things used in this project

Hardware components

Jumper wires (generic)
Jumper wires (generic)
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1
Arduino UNO
Arduino UNO
×1
Amazon Web Services button
×1
Resistor 10k ohm
Resistor 10k ohm
×1
9V battery (generic)
9V battery (generic)
×1
9V Battery Clip
9V Battery Clip
×1

Story

Read more

Schematics

Arduino 1 Project Wiring

To get button to run. Make sure to include power supply.

Code

Arduino Project 1 Code

Arduino
Button one moves motor 30 degrees, Button 2 moves motor 90 degrees, and Potentiometer moves motor between 0 and 180 degrees.
#include <Servo.h>   // Called another code
//Alex Cockman's Code
Servo myservo; 

int Pos = 0;   // Staring at pos 0, sets a starting point
int button1 = 4; // Declares button 1 in digital Pin 4
int button2 = 5; // Declares button 2 in digital Pin 5
int potpin = 0; 
int val; 

void setup() {
 myservo.attach(9); // Puts the servo motor into pin 9
 pinMode(12, INPUT_PULLUP); 
 pinMode(2, INPUT_PULLUP);
}

void loop() {   // Alex Cockman 
  val = analogRead(potpin); // tells to read value given
  val = map(val, 0, 1023, 0, 180);
  myservo.write(val);
  delay(1000);  // puts in a 1 second delay

int button1State = digitalRead(button1);   // Reads which button is presssed if any
int button2State = digitalRead(button2);

if(button1State == LOW)  // If button is pressed then run if statement
{
  for (Pos = 0; Pos <= 30; Pos += 30) {     // run motor 30 degrees from button 1
    myservo.write(Pos);
    delay(1000);

  }

}

if(button2State == LOW)
{
  for (Pos = 0; Pos <= 90; Pos += 90) {    // run motor 90 degrees from button 2
    myservo.write(Pos);
    delay(1000);

  }

}      //Alex Cockman
}

Credits

Alex Cockman

Alex Cockman

1 project • 4 followers
Thanks to Team 12.

Comments

Add projectSign up / Login