#define A0 pinbase + 0
#define A1 pinbase + 1
#define A2 pinbase + 2
#define A3 pinbase + 3
#define motoRPin1 2 // define the pin connected to L293D
#define motoRPin2 0
#define enablePin 3
// Map function: map the value from a range of mapping to another range.
long map(long value,long fromLow,long fromHigh,long toLow,long toHigh){
return (toHigh-toLow)*(value-fromLow) / (fromHigh-fromLow) + toLow;
}
//motor function: determine the direction and speed of the motor according to the ADC
value to be input.
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 Back...\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 the PWM duty cycle.
}
int main(void){
int value;
if(wiringPiSetup() == -1){ //when initialize wiring failed,print message to screen
printf("setup wiringPi failed !");
return 1;
}
pinMode(enablePin,OUTPUT);// set mode for the pin
pinMode(motoRPin1,OUTPUT);
pinMode(motoRPin2,OUTPUT);
softPwmCreate(enablePin,0,100);// define PWM pin