User Tools

Site Tools


user:bmurphy7:hpc0:motor

Motor

The Circuit:

The Code:

  1 /**********************************************************************                                     
  2 * Filename    : Motor.cpp
  3 * Description : Control Motor by L293D
  4 * Author      : www.freenove.com
  5 * modification: 2020/03/09
  6 **********************************************************************/
  7 #include <wiringPi.h>
  8 #include <stdio.h>
  9 #include <softPwm.h>
 10 #include <math.h>
 11 #include <stdlib.h>
 12 #include <ADCDevice.hpp>
 13  
 14 #define motorPin1    2                   //define the pin connected to L293D
 15 #define motorPin2    0
 16 #define enablePin    3
 17  
 18 ADCDevice *adc;                          // Define an ADC Device class object
 19  
 20 //Function to map the value from a range to another range.
 21 long map(long value,long fromLow,long fromHigh,long toLow,long toHigh){
 22     return (toHigh-toLow)*(value-fromLow) / (fromHigh-fromLow) + toLow;
 23 }
 24 //Function to determine the direction and speed of the motor 
 25 void motor(int ADC){
 26     int value = ADC -128;
 27     //are we going forward
 28     if(value>0){
 29         digitalWrite(motorPin1,HIGH);
 30         digitalWrite(motorPin2,LOW);
 31         printf("turn Forward...\n");
 32     }
 33     //are we going backwards
 34     else if (value<0){
 35         digitalWrite(motorPin1,LOW);
 36         digitalWrite(motorPin2,HIGH);
 37         printf("turn Back...\n");
 38     }
 39     //or are we stopping
 40     else {
 41         digitalWrite(motorPin1,LOW); 
 42         digitalWrite(motorPin2,LOW);
 43         printf("Motor Stop...\n");
 44     }
 45     softPwmWrite(enablePin,map(abs(value),0,128,0,100));
 46     printf("The PWM duty cycle is %d%%\n",abs(value)*100/127);
 47 }  
 48    
 49    
 50 int main(void){
 51     adc = new ADCDevice();
 52     int value;                           // Tells us the hardware value 
 53     printf("Program is starting ... \n");
 54     
 55     if(adc->detectI2C(0x48)){            // Are we using the pcf8591.
 56         delete adc;                      // Reset adc
 57         adc = new PCF8591();             // Put in what hardware we are using
 58     }    
 59     else if(adc->detectI2C(0x4b)){       // Are we using the ads7830
 60         delete adc;                      // Reset the adc
 61         adc = new ADS7830();             // Put in what hardware we are using
 62     }    
 63     else{
 64         printf("No correct I2C address found, \n"
 65         "Please use command 'i2cdetect -y 1' to check the I2C address! \n"
 66         "Program Exit. \n");
 67         return -1;
 68     }   
 69     wiringPiSetup();
 70     pinMode(enablePin,OUTPUT);           // Set mode for the pin
 71     pinMode(motorPin1,OUTPUT);
 72     pinMode(motorPin2,OUTPUT);
 73     softPwmCreate(enablePin,0,100);      // Define PMW pin
 74     while(1){
 75         value = adc->analogRead(0);  // Read analog value of A0 pin
 76         printf("ADC value : %d \n",value);
 77         motor(value);                    // Rotate the motor
 78         delay(100);
 79     }    
 80     return 0;
 81 }

In this project I had to setup the pcf8591 once more swapping where a few of the jumpers went according to the diagram. Otherwise I had to hook up an external power supply to the board, an L293D, a rotary potentiometer, and a motor as well. When I finally hooked up the circuit, turn on the external power, and ran the program the motor started buzzing for a little bit, going silent, then buzzing again but in a different direction. Sadly when I tried to control it with the potentiometer, but that was not working for some reason. Otherwise I aligned the comments in the code, made some of the comments provide more a clear cut information when dealing with the functions, and I moved the declaration of variables to the outside of the while loop.

user/bmurphy7/hpc0/motor.txt · Last modified: 2020/05/12 00:37 by 127.0.0.1