Search code examples
cprintfencoderpwmwiringpi

How can I print a digitalRead using wiringPi in a C programm


I am trying to run a program for reading an encoder in a motor. The PWM is running correctly but I would like to read the input from the encoder. I am using digitalRead and I already tried with printf(digitalRead(21)) but it does not really work. How can I visualize it? I attach my program in C:

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#define MFOR_MOTOR 25
#define MBACK_MOTOR 24 
#define PWM_MOTOR 1
#define ENCODER_A 21
#define ENCODER_B 22

int main(void) {

    if (wiringPiSetup() == -1)
        exit(1);

    int pwm_user;
    wiringPiSetup();
    pinMode(MFOR_MOTOR, OUTPUT);
    pinMode(MBACK_MOTOR, OUTPUT);
    pinMode(PWM_MOTOR, PWM_OUTPUT);
    pinMode(ENCODER_A, INPUT);
    pinMode(ENCODER_B, INPUT);

    printf("Raspberry Pi wiringPi Motor PWM program\n") ;
    printf("PWM from motor: ");
    scanf("%d", &pwm_user); 
    pwmWrite(PWM_MOTOR, pwm_user);
    digitalWrite(MFOR_MOTOR, HIGH);

    while (1) {
        digitalRead(ENCODER_A);
    }

    digitalWrite(MFOR_MOTOR, LOW);
    return 0;  
}

Solution

  • You can convert the integer to a string with:

    printf("%d\n", digitalRead(21));