电机+1舵机+红外控制+灯+LCD

Posted 卓源君

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了电机+1舵机+红外控制+灯+LCD相关的知识,希望对你有一定的参考价值。

 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <IRremote.h>     // IRremote库声明 
#include <pt.h>//ProtoThreads必须包含的头文件
#include <Servo.h>
#include <Wire.h> 
#include <LCD.h> 
#include <LiquidCrystal_I2C.h> 



#define PT_USE_TIMER
#define PT_USE_SEM

Servo myservo;
LiquidCrystal_I2C lcd(0x20,2,1,0,4,5,6,7);

int input1 = 9; // 定义uno的pin 5 向 input1 输出
int input2 = 10; // 定义uno的pin 6 向 input2 输出
int input3 = 5; // 定义uno的pin 9 向 input3 输出
int input4 = 6; // 定义uno的pin 10 向 input4 输出
int input7 = 7; // 定义uno的pin 7 向 input7 输出
int pos = 20;

int LED = 4;
int RECV_PIN = 3;
IRrecv irrecv(RECV_PIN);
decode_results results;

static struct pt thread1;//thread one


long control[7][3] = {//遥控器矫正数字
  {16580863, 16613503, 16597183},
  {16589023, 16621663, 16605343},
  {16584943, 16617583, 16601263},
  {16593103, 16625743, 16609423},
  {16582903, 16615543, 16599223},
  {16591063, 16623703, 16607383},
  {16586983, 16619623, 16603303}
};

void initRun() { // init
  pinMode(input1, OUTPUT);
  pinMode(input2, OUTPUT);
  pinMode(input3, OUTPUT);
  pinMode(input4, OUTPUT);
  initLeftAndRight();
}

void initLight() { //init light
  pinMode(LED, OUTPUT);
}

void getLeftAndRight_left() {
  for (pos = 30; pos < 90; pos += 1)
  {
    myservo.write(pos);
    delay(15);
  }
}
void getLeftAndRight_center() {
  Serial.println(110);
  Serial.println(pos);
  if (pos == 90) {
    for (pos = 90; pos > 30; pos -= 1)
    {
      myservo.write(pos);
      delay(15);
    }
  }else if(pos == -30){
    for (pos = -30; pos < 30; pos += 1)
    {
      myservo.write(pos);
      delay(15);
    }
  }

}
void getLeftAndRight_right() {
  for (pos = 30; pos > -30; pos -= 1)
  {
    myservo.write(pos);
    delay(15);
  }
}

void initLeftAndRightPoint() {
  myservo.write(pos);
}
void initLeftAndRight() {
  myservo.attach(input7);
  initLeftAndRightPoint();
}
void wjf_initLcd(){
  lcd.begin (16,2); // for 16 x 2 LCD module 
  lcd.setBacklightPin(3,POSITIVE); 
  lcd.setBacklight(HIGH);
}
void wjf_setLcd(char *str){
  lcd.home (); // set cursor to 0,0 
  lcd.print(str); 
  lcd.setCursor (0,1); // go to start of 2nd line 
  //lcd.print(millis()); 
  //delay(1000); 
  lcd.setBacklight(LOW); // Backlight off delay(250);        
  lcd.setBacklight(HIGH); // Backlight on delay(1000); 
}
void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // 启动接收器
  initRun();
  initLight();
  wjf_initLcd();
  wjf_setLcd("welcome human");
}

void left() {
  digitalWrite(input1, HIGH); //给高电平
  digitalWrite(input2, LOW); //给低电平
  //delay(1000);   //延时1秒
}
void right() {
  digitalWrite(input1, LOW);
  digitalWrite(input2, HIGH);
  //delay(1000);
}
void goRun() {
  digitalWrite(input3, HIGH); //给高电平
  digitalWrite(input4, LOW); //给低电平
  //delay(1000);   //延时1秒
}
void goBack() {
  digitalWrite(input3, LOW);
  digitalWrite(input4, HIGH);
  //delay(1000);
}
void goStop() {
  //stop 停止
  digitalWrite(input1, LOW);
  digitalWrite(input2, LOW);
  digitalWrite(input3, LOW);
  digitalWrite(input4, LOW);
  //delay(1000);  //延时0.5秒
}
void openLight() {
  digitalWrite(LED, HIGH);
}
static int goThread1(struct pt *pt) //线程
{
  //PT_BEGIN(pt);  //线程开始
  while (1) //每个线程都不会死
  {
    digitalWrite(LED, HIGH);
    //delay(1000);
    digitalWrite(LED, LOW);
    //delay(1000);
  }
  //PT_END(pt); //线程结束
}



void closeLight() {
  digitalWrite(LED, LOW);
}



void loop() {
  if (irrecv.decode(&results))
  {
    Serial.println(results.value, HEX);//以16进制换行输出接收代码
    if (results.value == 4294967295) {
      //long click

    } else {
      if (results.value == control[0][0]) {

      } else if (results.value == control[0][1]) {////openLight();
        //goRun();
        openLight();
        left();
        wjf_setLcd("go forward");
      } else if (results.value == control[0][2]) {

      } else if (results.value == control[1][0]) {//
        openLight();
        getLeftAndRight_left();
        wjf_setLcd("go left");
      } else if (results.value == control[1][1]) {//
        initLeftAndRightPoint();
        closeLight();
        goStop();
        //
        openLight();
        getLeftAndRight_center();
        wjf_setLcd("stoping...");
      } else if (results.value == control[1][2]) {//
        openLight();
        getLeftAndRight_right();
        wjf_setLcd("go right");
      } else if (results.value == control[2][0]) {

      } else if (results.value == control[2][1]) {////openLight();
        //goBack();
        openLight();
        right();
        wjf_setLcd("go back");
      } else if (results.value == control[2][2]) {

      } else if (results.value == control[3][0]) {//0
        
      } else if (results.value == control[3][1]) {
        
      } else if (results.value == control[3][2]) {//st
        
      } else if (results.value == control[4][0]) {

      } else if (results.value == control[4][1]) {


      } else if (results.value == control[4][2]) {


      } else if (results.value == control[5][0]) {


      } else if (results.value == control[5][1]) {


      } else if (results.value == control[5][2]) {


      } else if (results.value == control[6][0]) {


      } else if (results.value == control[6][1]) {


      } else if (results.value == control[6][2]) {


      }
    }
    irrecv.resume(); // 接收下一个值
  }
  delay(100);
}

 

以上是关于电机+1舵机+红外控制+灯+LCD的主要内容,如果未能解决你的问题,请参考以下文章

51单片机 超声波红外避障 程序

Arduino控制舵机详解(含代码)

舵机控制那些事(附STM32代码!!!)

如何用单片机控制液晶显示器呀,怎么接线……

Proteus仿真51单片机+按键控制步进电机驱动+LCD1602显示

arduino 红外遥控器控制LED灯