Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combining Project 13.1 and 20.1 to have the motor speed displayed on I2C LCD #34

Open
atlantiscoder opened this issue Dec 30, 2021 · 0 comments

Comments

@atlantiscoder
Copy link

Hi, I am a novice programmer trying to achieve the following by combining the codes for the projects 13.1 Control a DC Motor with a Potentiometer and Project 20.1 I2C LCD1602 to have the I2C LCD display the speed that is shown on the terminal window when executing the project 13.1.

I've attached a code to first display the time while running the motor with a potentiometer but I can't get the code to compile... Can anyone guide me to achieve this? and teach me what I am doing wrong?

-------- Code ---------------

#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <stdio.h>
#include <stdlib.h>
#include <softPwm.h>
#include <math.h>
#include <stdlib.h>
#include <ADCDevice.hpp>
#include <pcf8574.h>
#include <lcd.h>
#include <time.h>

int pcf8574_address = 0x27; // PCF8574T:0x27, PCF8574AT:0x3F
#define BASE 64 // BASE any number above 64
//Define the output pins of the PCF8574, which are directly connected to the LCD1602 pin.
#define RS BASE+0
#define RW BASE+1
#define EN BASE+2
#define LED BASE+3
#define D4 BASE+4
#define D5 BASE+5
#define D6 BASE+6
#define D7 BASE+7

#define motorPin1 2 //define the pin connected to L293D
#define motorPin2 0
#define enablePin 3

ADCDevice *adc; // Define an ADC Device class object

//Map function: map the value from a range 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
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 PMW duty cycle
}

int lcdhd;// used to handle LCD

void printDataTime(){//used to print system time
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);// get system time
timeinfo = localtime(&rawtime);//convert to local time
printf("%s \n",asctime(timeinfo));
lcdPosition(lcdhd,0,1);// set the LCD cursor position to (0,1)
lcdPrintf(lcdhd,"Time:%02d:%02d:%02d",timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec); //Display system time on LCD
}
int detectI2C(int addr){
int _fd = wiringPiI2CSetup (addr);
if (_fd < 0){
printf("Error address : 0x%x \n",addr);
return 0 ;
}
else{
if(wiringPiI2CWrite(_fd,0) < 0){
printf("Not found device in address 0x%x \n",addr);
return 0;
}
else{
printf("Found device in address 0x%x \n",addr);
return 1 ;
}
}
}

int main(void){

int i;

printf("Program is starting ...\n");

wiringPiSetup();
if(detectI2C(0x27)){
    pcf8574_address = 0x27;
}else if(detectI2C(0x3F)){
    pcf8574_address = 0x3F;
}else{
    printf("No correct I2C address found, \n"
    "Please use command 'i2cdetect -y 1' to check the I2C address! \n"
    "Program Exit. \n");
    return -1;
}
pcf8574Setup(BASE,pcf8574_address);//initialize PCF8574
for(i=0;i<8;i++){
    pinMode(BASE+i,OUTPUT);     //set PCF8574 port to output mode
}
digitalWrite(LED,HIGH);     //turn on LCD backlight
digitalWrite(RW,LOW);       //allow writing to LCD
lcdhd = lcdInit(2,16,4,RS,EN,D4,D5,D6,D7,0,0,0,0);// initialize LCD and return “handle” used to handle LCD
if(lcdhd == -1){
    printf("lcdInit failed !");
    return 1;
}
while(1){

    printDataTime();        // print system time
    delay(1000);
}
return 0;

adc = new ADCDevice();
printf("Program is starting ... \n");

if(adc->detectI2C(0x48)){    // Detect the pcf8591.
    delete adc;                // Free previously pointed memory
    adc = new PCF8591();    // If detected, create an instance of PCF8591.
}
else if(adc->detectI2C(0x4b)){// Detect the ads7830
    delete adc;               // Free previously pointed memory
    adc = new ADS7830();      // If detected, create an instance of ADS7830.
}
else{
    printf("No correct I2C address found, \n"
    "Please use command 'i2cdetect -y 1' to check the I2C address! \n"
    "Program Exit. \n");
    return -1;
}
wiringPiSetup();
pinMode(enablePin,OUTPUT);//set mode for the pin
pinMode(motorPin1,OUTPUT);
pinMode(motorPin2,OUTPUT);
softPwmCreate(enablePin,0,100);//define PMW pin
while(1){
    int value = adc->analogRead(0);  //read analog value of A0 pin
    printf("ADC value : %d \n",value);
    motor(value);        //make the motor rotate with speed(analog value of A0 pin)
    delay(100);
}
return 0;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant