Friday, October 30, 2015

Robot part 1

For this project we are making a robot that has to satisfy these requirements:

  1. Go forward for 3 seconds
  2. Roam randomly
  3. Precision Steering
  4. Wide turn & Spin
  5. Turn signals

So far we have only completed the first two steps so our Robot can go forward and then stop after three seconds and then can also roam completely randomly.

For the code we have made three functions:
  1. Go forward
  2. Turn right
  3. Turn left

The go forward function:

//go forward function
int goForward(){
  int counter = 0;
  int counterFinal = 3000;
  boolean breakFunction = false;
  
  while(true){
    if(counter<counterFinal){
      left.write(180);
      right.write(0);
    }
    
    if(counter>(counterFinal-1)){
      left.write(90);
      right.write(90);
      break;
      
    }
    counter += 1;
    delay(1);
    
  }
}


The turn right function:
//turn right function
int turnRight(){
  int counter = 0;
  int counterFinal = 3000;
  boolean breakFunction = false;
  
  while(true){
    if(counter<counterFinal){
      left.write(180);
      right.write(90);
    }
    
    if(counter>(counterFinal-1)){
      left.write(90);
      right.write(90);
      break;
      
    }
    counter += 1;
    delay(1);
    
  }
}


The turn left function:
//turn left function
int turnLeft(){
  int counter = 0;
  int counterFinal = 3000;
  boolean breakFunction = false;
  
  while(true){
    if(counter<counterFinal){
      left.write(90);
      right.write(0);
    }
    
    if(counter>(counterFinal-1)){
      left.write(90);
      right.write(90);
      break;
      
    }
    counter += 1;
    delay(1);
    
  }
}


The main loop:
void loop(){
  
  int randomNumber = random(3);
  
  if(randomNumber == 0){
    turnRight();
  }
  if(randomNumber == 1){
    turnLeft();
  }
  if(randomNumber == 2){
    goForward();
  }
  claw.write(0);
  delay(1);
  
}


The main loop chooses one of three numbers: 0, 1, or 2. Depending on which number it picks the robot will do one of three things: turn right, turn left, or go forward. After completing the function it will repeat this endlessly and become a random roaming robot.

Pictures:



Making a robot like this one is a great way to put together all the things that we have learned in robotics and will help me in the future because it is good practice on how to put all these parts together to make something bigger.

Changing pitch with a Potentiometer

For this project we used a Potentiometer to change the pitch that our Piezo was putting out.

We used this code plus the pitches.h code to make the sound:

int playTone = 0;

int tonePin = 8;

int inputPin = A0;

void setup(){
  pinMode(inputPin,INPUT);
  pinMode(tonePin,OUTPUT);
  Serial.begin(9600);
}

void loop(){
  playTone = map(analogRead(inputPin),0,600,60,1500);
  Serial.println(analogRead(inputPin));
  
  
  tone(tonePin,playTone,10);
}

In this code we have an analog input from the potentiometer that gives a value between 0 and 1024. We cut down this number because we were only getting values between 0 and 600. This will make the sound more precise. We then mapped this number between the values 60 and 1500 which is any sound between a very very low rumble to a super high pitched "fly-buzzing" like sound. Then we outputted this to the Piezo and the sound would change whenever we turned the potentiometer.

Pictures:
The Piezo


The Potentiometer

In the future, knowing how to use a Piezo is only really important if you are going to be working with sound or sound processors or the like. Knowing how to use a potentiometer is a very important skill for robotics because it is one of the core parts of robotics.

Learning how to drive a Servo

This Code makes the Servo switch between 2 points in the 180 degree range repeatedly.

#include <Servo.h>

boolean buttonCheck = false;

int buttonTimer = 0;

int servoAmount = 1700;

int valid = LOW;

int pin = 8;

int MAX = 5000;

int MIN = 600;

Servo servo1;

void setup(){
  servo1.attach(2);
  pinMode(pin,INPUT);
}

void loop(){
  
  valid = digitalRead(pin);
  
  if (buttonCheck == false) {
    if (valid == HIGH) {
      
      //turn on the fact that the button is being pushed
      buttonCheck = true;
      
      //switches the variable controlling the position of servo
      if (servoAmount == MAX) {
        servoAmount = MIN;
      }
      else {
        servoAmount = MAX;
      }
    }
  }
  
  //timer for the button check
  if (buttonCheck == true) {
    // should add 1 microsecond to the timer
    buttonTimer = buttonTimer + 1;
    
    //stops the timer once it gets to half a second
    if (buttonTimer == 500) {
      buttonTimer = 0;
      buttonCheck = false;
    }
  }
    
  
  //controls where the servo should be
  //servo should run 1700 to 1200
  servo1.writeMicroseconds(servoAmount);
  
  //delays 1 microsecond
  delay(1);

}

A picture of the Servo that we used:


In the future we can use the code that we used to drive the Servo's to drive speed controllers for motors and also continuous servos. Learning how to drive a Servo is an important skill in robotics.