找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

帖子
查看: 2445|回復: 2
打印 上一主題 下一主題
收起左側(cè)

esp8266控制Arduinol led燈亮滅的程序,想加一個紅外感應功能

[復制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:395068 發(fā)表于 2018-10-8 18:17 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
會編Arduino的請看看
上面是手機通過esp8266控制Arduinol  led燈亮滅的程序,想在這個基礎(chǔ)上加一個紅外感應的,人靠近,燈亮,人離開,燈滅,請問應該怎么加,試了好幾種方法,都不行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空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發(fā)
ID:277550 發(fā)表于 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 發(fā)表于 2018-10-9 00:20 | 只看該作者
Timer1要改成這樣的
。。~~~~~~
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表