找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

帖子
查看: 2459|回復(fù): 2
收起左側(cè)

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

[復(fù)制鏈接]
ID:395068 發(fā)表于 2018-10-8 18:17 | 顯示全部樓層 |閱讀模式
會編Arduino的請看看
上面是手機通過esp8266控制Arduinol  led燈亮滅的程序,想在這個基礎(chǔ)上加一個紅外感應(yīng)的,人靠近,燈亮,人離開,燈滅,請問應(yīng)該怎么加,試了好幾種方法,都不行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");
  }
  }


回復(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
        }
}


回復(fù)

使用道具 舉報

ID:277550 發(fā)表于 2018-10-9 00:20 | 顯示全部樓層
Timer1要改成這樣的
。。~~~~~~
回復(fù)

使用道具 舉報

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

本版積分規(guī)則

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

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

快速回復(fù) 返回頂部 返回列表