Eric Chen
Published

Homework 8: Final Vehicle

The second project draws to a close.

IntermediateShowcase (no instructions)3,629
Homework 8: Final Vehicle

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
L298 motor controller
×1
9V battery (generic)
9V battery (generic)
×1
AA battery
×4
lollipop sticks
×1
Epoxy
×1
orange arcylic
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1
push button switch
×2
Resistor 1k ohm
Resistor 1k ohm
×2
DC motor (generic)
×2
rubber wheel
×2
Arduino Proto Shield
Arduino Proto Shield
×1
solid core wire
×1
stranded wire
×1

Hand tools and fabrication machines

Laser cutter (generic)
Laser cutter (generic)
Soldering iron (generic)
Soldering iron (generic)
Wire cutter

Story

Read more

Schematics

Cut Diagram

Circuitry

Code

Code

C/C++
const int LEFT_IN = 3;
const int LEFT_OUT = 5;
const int RIGHT_OUT = 6;
const int RIGHT_IN = 9;

const int POTENTIOMETER = 0;
const int BACKWARD_SWITCH = 11;
const int FORWARD_SWITCH = 12;

void setup()  {
  Serial.begin(9600);
  pinMode(POTENTIOMETER, INPUT);
  pinMode(BACKWARD_SWITCH, INPUT);
  pinMode(FORWARD_SWITCH, INPUT);
}

void loop() {
  int val = analogRead(POTENTIOMETER);
  int left_val = 255;
  int right_val = 255;
  if (val < 475) {
    left_val = (val >= 150) ? map(val, 150, 475, 0, 255) : 0;
  } else if (val > 525) {
    right_val = (val <= 850) ? 255 - map(val, 525, 850, 0, 255) : 0;
  }
  if (digitalRead(FORWARD_SWITCH) == HIGH) {
    move_wheel(LEFT_IN, LEFT_OUT, left_val, true, true);
    move_wheel(RIGHT_IN, RIGHT_OUT, right_val, true, false);
  } else if (digitalRead(BACKWARD_SWITCH) == HIGH) {
    move_wheel(LEFT_IN, LEFT_OUT, left_val, false, true);
    move_wheel(RIGHT_IN, RIGHT_OUT, right_val, false, false);
  } else {
    move_wheel(LEFT_IN, LEFT_OUT, 0, true, true);
    move_wheel(RIGHT_IN, RIGHT_OUT, 0, true, false);
  }
}

void move_wheel(int in, int out, int val, boolean forward, boolean left) {
  Serial.print((left) ? "left" : "right");
  Serial.print(": ");
  Serial.print((forward) ? val : -val);
  Serial.print("\n");
  
  analogWrite(in, (forward) ? val : 0);
  analogWrite(out, (forward) ? 0 : val);
  
  return;
}

Credits

Eric Chen

Eric Chen

9 projects • 1 follower
EECS student at UC Berkeley

Comments