EasyManua.ls Logo

Freenove Ultimate Starter Kit

Freenove Ultimate Starter Kit
286 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
Chapter 13 Motor & Driver
148
www.freenove.com
support@freenove.com
54
55
56
57
58
59
60
61
62
63
pcf8591Setup(pinbase,address);//initialize PCF8591
while(1){
value = analogRead(A0); //read A0 pin
printf("ADC value : %d \n",value);
motor(value); // start the motor
delay(100);
}
return 0;
}
We have been familiar with reading ADC value. So, let’s learn directly subfunction void motor(int ADC): first,
compare ADC value with 128 (value corresponding to midpoint). When the current ADC value is higher,
motoRPin1 outputs high level and motoRPin2 outputs low level to control motor to run with forward rotation
direction. When the current ADC value is lower, motoRPin1 outputs low level and motoRPin2 outputs high
level to control motor run with reversed direction. When the ADC value is equal to 128, motoRPin1 and
motoRPin2 output low level, then the motor stops. And then determine PWM duty cycle according to the
difference between ADC value and 128. Because the absolute difference value stays within 0-128. We need
to use the map() subfunction mapping the difference value to range of 0-255. Finally print out the duty cycle.
void motor(int ADC){
int value = ADC -128;
if(value>0){
digitalWrite(motoRPin1,HIGH);
digitalWrite(motoRPin2,LOW);
printf("turn Forward...\n");
}
else if (value<0){
digitalWrite(motoRPin1,LOW);
digitalWrite(motoRPin2,HIGH);
printf("turn Backward...\n");
}
else {
digitalWrite(motoRPin1,LOW);
digitalWrite(motoRPin2,LOW);
printf("Motor Stop...\n");
}
softPwmWrite(enablePin,map(abs(value),0,128,0,100));
printf("The PWM duty cycle is %d%%\n",abs(value)*100/127);// print out PWM duty
cycle.
}

Table of Contents