Skip to content

March 16th: In Review

March 16, 2011

Hi All! We made not one, not two, but THREE codes today in class.  All of these codes involve controlling a motor via a TIP122 transistor.

For the afternoon, get cracking on mid-term projects.

// FIRST SKETCH
// a motor cycles between speeding up and slowing down. Input from a potentiometer
// controls the rate at which this acceleration/decelration occurs
#define MOTOR 3
#define POT 0
void setup(){
pinMode(MOTOR, OUTPUT);
}
void loop(){
int motor_speed;
int pot_val;
int acceleration;     // how fast the motor speeds up or slows down
for (motor_speed = 0; motor_speed < 255; motor_speed++){
analogWrite(MOTOR, motor_speed);
// update acceleration value from potentiometer
pot_val = analogRead(POT);
acceleration = map(pot_val, 0, 1024, 5, 50);
delay(acceleration);
}
for (motor_speed = 255; motor_speed > 0; motor_speed--){
analogWrite(MOTOR, motor_speed);
// update acceleration value from potentiometer
pot_val = analogRead(POT);
acceleration = map(pot_val, 0, 1024, 5, 50);
delay(acceleration);
}
}
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

// SECOND SKETCH
// this simple sketch controls the speed of the motor directly from
// the potentiometer knob

define POT 0

define MOTOR 3

void setup(){

pinMode(MOTOR, OUTPUT);

}

void loop(){

int motor_speed;

int pot_val;

pot_val = analogRead(POT;

motor_speed = map(pot_val, 0, 1024, 70, 230);

analogWrite(MOTOR,motor_speed);

}
//XXXXXXXXXXXXXXXXXXXXX


//THIRD SKETCH

// utilizes the random function to make our motor randomly accelerate

// or decelerate

 

#define MOTOR 3

int motor_speed = 128;

 

 

void setup(){

pinMode(MOTOR, OUTPUT);

 

 

}

 

void loop(){

int change_amt;

int random_num;

 

 

random_num = random(1000);

change_amt = map(random_num, 0, 1000, -5, 5);

motor_speed = (motor_speed + change_amt);

motor_speed = constrain(motor_speed, 0, 255);

delay(20);

analogWrite(MOTOR, motor_speed);

 

}



Advertisements
2 Comments leave one →
  1. July 5, 2013 7:11 am

    Hello would you mind sharing which blog platform you’re working with? I’m looking to start my own
    blog in the near future but I’m having a hard time selecting between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design seems different then most blogs and I’m looking
    for something completely unique. P.S Apologies for being off-topic but I had to ask!

  2. July 8, 2013 1:01 pm

    Curtis – this is just a wordpress blog – nothing too special – I also use Drupal for a lot of things as well, but have found that wordpress works pretty good for stuff like this –

    good luck!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: