树梅派学习 12. 光隔离传感器实验

版权声明:(谢厂节的博客)博主文章绝大部分非原创,转载望留链接。 https://blog.csdn.net/xundh/article/details/81876697

电路图:
这里写图片描述

接线图:
这里写图片描述

代码:

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

#define LBPin       0  // light break pin set to GPIO0
#define Gpin        1
#define Rpin        2

void LED(int color)
{
    pinMode(Gpin, OUTPUT);
    pinMode(Rpin, OUTPUT);
    if (color == 0){
        digitalWrite(Rpin, HIGH);
        digitalWrite(Gpin, LOW);
    }
    else if (color == 1){
        digitalWrite(Rpin, LOW);
        digitalWrite(Gpin, HIGH);
    }
}

void Print(int x){
    if ( x == 0 ){
        printf("Light was blocked\n");
    }
}

int main(void){

    if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen
        printf("setup wiringPi failed !");
        return 1; 
    }

    pinMode(LBPin, INPUT);
    int temp;
    while(1){
        //Reverse the input of LBPin
        if ( digitalRead(LBPin) == 0 ){  
            temp = 1;
        }
        if ( digitalRead(LBPin) == 1 ){
            temp = 0;
        }

        LED(temp);
        Print(temp);
    }
    return 0;
}

编译运行:

gcc photo_interrupter.c -o photo_interrupter -lwiringPi -lpthread

实体图:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/xundh/article/details/81876697