Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Jessica Chang
Created November 10, 2015

Second Project Final Week

Showcase (no instructions)71
Second Project Final Week

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
9V battery (generic)
9V battery (generic)
×1
aa battery
×1
metal axel
×1
gears
×2
screws
×6
wheels
×6
lock pin and column
×1
econowood
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1
push button switch
×1
Arduino Proto Shield
Arduino Proto Shield
×1
solid core
×1
stranded wire
×1

Story

Read more

Code

Knob-trial.ino

Java
/* 
 Controlling a servo position using a potentiometer (variable resistor) 
 by Michal Rinott <http://people.interaction-ivrea.it/m.rinott> 

 modified on 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Knob
*/

#include <Servo.h>

Servo steer;  // create servo object to control a servo
Servo power;
//Servo myservo2;
const int backBtn = 2; 
const int forwardBtn = 3;
int forwardBtnState = 0;
int backBtnState = 0;

int potpin = 0;  // analog pin used to connect the potentiometer
int potentVal;    // value of potentionmeter

void setup()
{
  Serial.begin(9600);
  steer.attach(9);  //attaches the steering servo on pin 9 to the servo object
  power.attach(8);  // attaches the power servo on pin 8 to the servo object
  power.write(180);
  
  pinMode(forwardBtn, INPUT_PULLUP); //taking forward button as input
  pinMode(backBtn, INPUT_PULLUP);    //taking backward button as input
}

void loop() 
{ 
  forwardBtnState = digitalRead(forwardBtn);  // read forward button state-- 1 or 0 
  backBtnState = digitalRead(backBtn);        // read backward button state-- 1 or 0
  if (forwardBtnState == HIGH && backBtnState == LOW) { //if forward button is pressed, and backward button is released
    power.write(180);
    delay(5);
  } else if (backBtnState == HIGH && forwardBtnState == LOW) {  //if backbutton is pressed and forwards button is released
    power.write(150);
    delay(5);
  } else {
    power.write(160);
    delay(5);
  }
  potentVal = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  potentVal = map(potentVal, 0, 1023, 60, 120);     // scale it to use it with the servo (value between 0 and 180) 
  steer.write(potentVal);                   //maps val to steering servo
  delay(5);                           // waits for the servo to get there 

} 

Credits

Jessica Chang
12 projects • 0 followers
jessmchang.com
Contact

Comments

Please log in or sign up to comment.