继电器组
继电器组其实就是单个继电器的组合,在涉及使用继电器控制多个电器的时候,使用继电器组往往是最合适的选择。
继电器组的控制
当继电器组收到低电平时会点亮相应的led灯,反之若给它一个高电平,会使led灯熄灭。
配置步骤
1.初始化引脚
2.引脚io模式的配置
3.指定引脚的电平信号
示例代码
#include<wiringPi.h>
#include<stdio.h>
#include<string.h>
#define button1 6
#define button2 9
#define button3 10
#define button4 13
int main()
{
char cmd[12]={'\0'};
if(wiringPiSetup()==-1){
printf("init error\n");
return -1;
}
pinMode(button1,OUTPUT);
pinMode(button2,OUTPUT);
pinMode(button3,OUTPUT);
pinMode(button4,OUTPUT);
digitalWrite(button1,HIGH);
digitalWrite(button2,HIGH);
digitalWrite(button3,HIGH);
digitalWrite(button4,HIGH);
while(1){
printf("please input 1/2/3/4, off鏂紑 on瀵奸€氬紑鍏砛n");
memset(cmd,'\0',sizeof(cmd));
//scanf("%s",cmd);
gets(cmd);
if(strcmp(cmd,"1 on")==0){
digitalWrite(button1,LOW);
}
else if(strcmp(cmd,"1 off")==0){
digitalWrite(button1,HIGH);
}
else if(strcmp(cmd,"2 on")==0){
digitalWrite(button2,LOW);
}
else if(strcmp(cmd,"2 off")==0){
digitalWrite(button2,HIGH);
}
else if(strcmp(cmd,"3 on")==0){
digitalWrite(button3,LOW);
}
else if(strcmp(cmd,"3 off")==0){
digitalWrite(button3,HIGH);
}
else if(strcmp(cmd,"4 on")==0){
digitalWrite(button4,LOW);
}
else if(strcmp(cmd,"4 off")==0){
digitalWrite(button4,HIGH);
}
else if(strcmp(cmd,"all on")==0){
digitalWrite(button1,LOW);
digitalWrite(button2,LOW);
digitalWrite(button3,LOW);
digitalWrite(button4,LOW);
}
else if(strcmp(cmd,"all off")==0){
digitalWrite(button1,HIGH);
digitalWrite(button2,HIGH);
digitalWrite(button3,HIGH);
digitalWrite(button4,HIGH);
}
else{
printf("input erro\n");
}
}
return 0;
}