找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2070|回复: 2
打印 上一主题 下一主题
收起左侧

esp8266控制Arduinol led灯亮灭的程序,想加一个红外感应功能

[复制链接]
跳转到指定楼层
楼主
ID:395068 发表于 2018-10-8 18:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
会编Arduino的请看看
上面是手机通过esp8266控制Arduinol  led灯亮灭的程序,想在这个基础上加一个红外感应的,人靠近,灯亮,人离开,灯灭,请问应该怎么加,试了好几种方法,都不行int LED = 9;int Sensor = 7;
char ch[10];
void setup()
{
  pinMode(LED,OUTPUT);
  pinMode(Sensor,INPUT);

  Serial.begin(115200);
}
void loop()
{
  if(Serial.available())
  {
  for(int i=0;i<9;i++)
  {
    ch[i]=char(Serial.read());
    delay(1);
  }
  if(strncmp(ch,"1",1)==0)
  {
    digitalWrite(LED,HIGH);
  Serial.println("1");
  }
else if(strncmp(ch,"2",1)==0)
{
  digitalWrite(LED,LOW);
  Serial.println("2");
  }
  }


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:277550 发表于 2018-10-9 00:14 | 只看该作者
---------------只方便作延时熄灯-------------

#include <TimerOne.h>
#define TIMER_US 100000                // 100mS set timer duration in microseconds
#define TICK_COUNTS 200                // 20S worth of timer ticks

volatile long tick_count = TICK_COUNTS;         // Counter for 20S

void setup(){
        pinMode(13, OUTPUT);
        attachInterrupt(0, blink, FALLING);
        // LOW to trigger the interrupt whenever the pin is low,
        // CHANGE to trigger the interrupt whenever the pin changes value
        // RISING to trigger when the pin goes from low to high,
        // FALLING for when the pin goes from high to low.       
       
        //Board         int.0 int.1
        //Uno, Ethernet 2     3
       
        Timer1.initialize(TIMER_US);                  // Initialise timer 1
        Timer1.attachInterrupt( timerIsr );           // attach the ISR routine h       
}

void loop(){
        //Add your task
}

void blink(){
        digitalWrite(13, HIGH);                // Toggle pin 13 HIGH
        tick_count = TICK_COUNTS;        // Reload
        timer1.restart();
}

// --------------------------
// timerIsr() 100 milli second interrupt ISR()
// Called every time the hardware timer 1 times out.
// --------------------------
void timerIsr(){   
        if (!(--tick_count)){                        // Count to 20S
                timer1.stop();
                digitalWrite(13, LOW);                // Toggle pin 13 LOW
        }
}


回复

使用道具 举报

板凳
ID:277550 发表于 2018-10-9 00:20 | 只看该作者
Timer1要改成这样的
。。~~~~~~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表