Skip to main content

Day 16: More Pointers | HBridge Motor Control

Hardware


Main Code: 

const int en=A0;
const int mc1=7;
const int mc2=8;
const int pot=A1;
const int pwm=5;
int val=0;
int velocity=0;

void setup() {
  // put your setup code here, to run once:
  pinMode(en,OUTPUT);
  pinMode(mc1,OUTPUT);
  pinMode(mc2,OUTPUT);
  pinMode(pwm,OUTPUT);
  brake();
}

void loop() {
  // put your main code here, to run repeatedly:
  val=analogRead(pot);
  if(val>562)
  {
    velocity=map(val,563,1023,0,255);
    forward(velocity);
  }

  else if(val < 462)
  {
    velocity=map(val, 461, 0, 0, 255);
    reverse(velocity);
  }

  else
  {
    brake();
  }
}

void forward(int rate)
{
  digitalWrite(en,LOW);
  digitalWrite(mc1,HIGH);
  digitalWrite(mc2,LOW);
  analogWrite(pwm,rate);
  digitalWrite(en,HIGH);
}

void reverse(int rate)
{
  digitalWrite(en,LOW);
  digitalWrite(mc1,LOW);
  digitalWrite(mc2,HIGH);
  analogWrite(pwm, rate);
  digitalWrite(en,HIGH);
}

void brake()
{
  digitalWrite(en,LOW);
  digitalWrite(mc1,LOW);
  digitalWrite(mc2,LOW);
  analogWrite(pwm,0);
  digitalWrite(en,HIGH);
}


Homework

Code for Hbridge motor controller
//Motor control code for Vishnay board

const int EN=A0; // Enable 
const int MC1=7; //Motor Control 1 
const int MC2=8; //Motor Control 2 
const int POT=1; //POT on Analog Pin 1 
const int PWM = 5; //PMW on pin 5 
int val = 0; //for storing the reading from the POT 
int previousval=0;
int velocity; //For storing the desired velocity (from 0-255) 
void setup() 
  pinMode(EN, OUTPUT); 
  pinMode(MC1, OUTPUT); 
  pinMode(MC2, OUTPUT); 
  pinMode(PWM, OUTPUT); 
  brake(); //Initialize with motor stopped 
  velocity=100;
  Serial.begin(9600);
  Serial.println("Begin Motor Control");
  Serial.println("Enter number for control option:");
  Serial.println("1. Stop");
  Serial.println("2. Forward");
  Serial.println("3. Reverse");
  //Serial.println("4. Read Current");
  Serial.println("4 Increase Speed");
  Serial.println("5. Decrease Speed");
void loop() 
  val = Serial.read();

  if (val=='1') 
  { 
    brake(); 
    previousval='1';
  } 

  else if (val=='2') 
  { 
  forward(velocity); 
  previousval='2';
  } 
  
  else if (val=='3')
  { 
  reverse(velocity);
  previousval='3';
  } 

  else if(val=='4')
  {
    velocity+=20;
    if(velocity<=255)
    {
       if(previousval=='2')
      {
        forward(velocity);
      }
      else if(previousval=='3')
      {
        reverse(velocity);
      }
    }
    else if(velocity>255)
    {
      velocity=255;
    }
    
    
  }

  else if(val=='5')
  {
    velocity-=20;
    if(previousval=='2')
    {
      forward(velocity);
    }
    else if(previousval=='3')
    {
      reverse(velocity);
    }
  }

//  else 
//  {
//    Serial.print("Enter valid motor control value");
//  }
//Motor goes forward at given rate (from 0-255) 
void forward (int rate) 
digitalWrite(EN, LOW); 
digitalWrite(MC1, HIGH); 
digitalWrite(MC2, LOW); 
analogWrite(PWM, rate); 
digitalWrite(EN, HIGH); 

//Motor goes backward at given rate (from 0-255) 
void reverse (int rate) 
digitalWrite(EN, LOW); 
digitalWrite(MC1, LOW); 
digitalWrite(MC2, HIGH); 
analogWrite(PWM, rate); 
digitalWrite(EN, HIGH); 

//Stops motor 
void brake () 
digitalWrite(EN, LOW); 
digitalWrite(MC1, LOW); 
digitalWrite(MC2, LOW); 
analogWrite(PWM, 0); 
digitalWrite(EN, HIGH); 
}

Comments

Popular posts from this blog

Day 20: Structures, Programming with Pointers

Lecture Structures Structure defines set of data, but individual parts do not have to be the same type. Example 1.1 struct hurricane {  char name[10];  int year,category; }; Within a structure, variables and even arrays can be defined. Structures are also known as aggregate data types since multiple data values can be collected into a single data type. Individual values within a structure are called data members, and each member is given a name. In Example 1.1, the names of the data members are name, year, and category. To refer to a data member, the structure variable name followed by a period and a data member name is used.  Definition and Initialization Define structure. Keyword struct used to define name of structure (aka structure tag) and data members that are included in structure After structure defined, structure variables can be defined using declaration statements. Semicolon required after structure definition. Statements can appear before m...

Day 6: Analog Sensors | Functions and Modularity

Functions Functions are sets of statements that typically perform operation or compute value. They can help to make programs more accessible and usable for non-programmers e.g. create function that allows user to type "go forward" and move a robot forward. Modules -Functions can be split up into modules, "divide and conquer" -Each module has specific purpose, can be written and tested separately -Smaller than complete solution, therefore testing is easier -Can be used in new problem solutions without being retested -Reduces overall length of program -Allows for increased collaboration; modules can be worked on in parallel Debugging Longer Programs Use a compiler that gives meaningful information about errors. Adding comments around some sections of code can allow for better focus on other parts of the program. Test complicated functions by themselves. Programmer Defined Functions Execution of program always begins with main function. Additional ...

Day 7: Random Numbers and Recursion | Sound

Lecture Random Numbers Random numbers can be generated using the rand function, however, the same value would be printed since it generates integers in a specified sequence. To generate a new sequence of random numbers each time, a new random-number seed is needed. First, I need to use srand to generate a seed in order for the rand function to generate new random numbers every time. We can also generate random integers between specified limits.  #include <stdio.h> #include <stdlib.h> int main(void) {   /* Declare variables and function prototype. */   unsigned int seed;   int a, b, k;   int rand_int(int a,int b);   /* Get seed value and interval limits. */   printf("Enter a positive integer seed value: \n");   scanf("%u",&seed);   srand(seed);   printf("Enter integer limits a and b (a<b): \n");   scanf("%i %i",&a,&b);   /* Generate and print ten random numbe...