Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Jessica Chang
Created November 3, 2015

Project 2 Part 2 : Working Chassis

"working" chassis

Showcase (no instructions)26
Project 2 Part 2 : Working Chassis

Things used in this project

Hardware components

Servos (Tower Pro MG996R)
×2
Jumper wires (generic)
Jumper wires (generic)
×17
metal dowels
×3
acryllic
×1
econo wood
×1
rubber bands
×8
9V battery (generic)
9V battery (generic)
×1
AA battery
×4
button switch
×2
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
Resistor 1k ohm
Resistor 1k ohm
×2

Hand tools and fabrication machines

Laser cutter (generic)
Laser cutter (generic)

Story

Read more

Code

Driving And Steering Code

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 forwardBtn = 2; 
const int backBtn = 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(160);
  pinMode(forwardBtn, INPUT_PULLUP); //taking forward button as input
  pinMode(backBtn, INPUT_PULLUP);    //taking backward button as input
}

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

} 

Credits

Jessica Chang

Jessica Chang

12 projects • 0 followers
jessmchang.com

Comments