Arduino造轮子—智能小夜灯

今天练习的程序是实现智能小夜灯的控制,首先,通过太极创客的视频来看看最终的实现结果:🍬

【太极创客】零基础入门学用Arduino 第三部分 智能应用篇 合辑

本次程序例程来自太极创客官网,此平台的Arduino教程深入浅出,对于想学习Arduino的同学,首推太极创客团队。(๑•̀ㅂ•́)و✧

以下便是今天练习的造轮子程序:

/*
 *基本功能介绍:
 * 用户可通过传感器控制RGB-LED点亮和关闭
 * 小夜灯配备人体红外感应传感器以及光敏电阻实现照明自动化
 * 点亮和熄灭时产生渐明渐暗效果
 * 点亮时色彩不断变化
 *
 * 基本电路连接说明:
 * 共阴极RGB-LED引脚R 连接 Arduino控制器引脚6
 * 共阴极RGB-LED引脚G 连接 Arduino控制器引脚5
 * 共阴极RGB-LED引脚B 连接 Arduino控制器引脚3
 * 红外人体感应模块信号输出引脚 连接 Arduino控制器引脚8
 * 光敏电阻分压电路信号输出引脚 连接 Arduino控制器引脚A0
*/

//宏定义引脚
#define rLedPin 6
#define gLedPin 5
#define bLedPin 3

#define irSensorPin 8
#define lightSensorPin A0

//led亮度
int ledR=0;
int ledG=0;
int ledB=0;

bool irReading; //红外人体感应模块输出
int lightRedaing; //光敏电阻分压电路信号输出
bool onOffState; //小夜灯开关状态

unsigned long previousIrMillis; //上一次检查红外传感器的时间
unsigned long previousLightMillis; //上一次检查光敏传感器的时间
int irCheckInterval=500; //红外传感器检查时间间隔
int irCheckInterval=1000; //光敏传感器检查时间间隔

int colorIndex; //颜色序列号
int colorChangeDelay=1; //颜色改变速度控制变量

void setup(){
    //设置引脚工作模式
    pinMode(rLedPin,OUTPUT);
    pinMode(gLedPin,OUTPUT);
    pinMode(bLedPin,OUTPUT);
    pinMode(irSensorPin,INPUT);
    
    Serial.begin(9600);
    Serial.println("Welcome!");
    Serial.println("===System Start Sensor Cheak===");
    
    //读取红外人体感应模块
    irReading=digitalRead(irSensorPin);
    lightRedaing=anologRead(lightSensorPin);
    
    Serial.println("===Checking Sensor===");
    Serial.print("irReading");Serial.println(irReading);
    Serial.print("lightRedaing");Serial.println(lightRedaing);
    Serial.println("======================");
    
}

//六个函数  
void loop(){
	unsigned long currentMillis=millis();//millis()函数可以用来获取Arduino开机后的运行时间
    
    irCheak(currentMillis);//检查红外传感器时间
    lightCheak(currentMillis);//检查光敏电阻时间
    
    if(irReading==HIGH){
        irCheckInterval=30000;
    }else{
        irCheckInterval=500;
    }
    
    if(irReading==HIGH&&lightRedaing>=880){
        if(onOffState==0)
            fadeOn();
        onOffState=1;
        
        if(colorIndex<=1535){
            colorIndex++;
        }else if(colorIndex>1535){
            colorIndex=0;
        }
        ledShowColor(colorIndex)
    }else{
        if(onOffState==1)
            fadeOff();
        onOffState=0;
    }
}


void irCheak(unsigned long currentMillis){
    if(currentMillis-previousIrMillis)>=irCheakInterval){
        irReading=digitalRead(irSensorPin);

    	Serial.println("===Checking IR Sensor===");
    	Serial.print("irReading=");Serial.println(irReading);
    	Serial.print("currentMillis");Serial.println(currentMillis);
       	Serial.print("previousIrMillis");Serial.println(previousIrMillis);
        Serial.println("=========================");
        previousIrMillis=currentMillis;
    }
}

void lightCheak(unsigned long currentMillis){
    if(currentMillis-previousLightMillis)>=lightCheakInterval){
		lightRedaing=anologRead(lightSensorPin);
        
    	Serial.println("===Checking Light Sensor===");
    	Serial.print("lightReading=");Serial.println(lightRedaing);
    	Serial.print("currentMillis");Serial.println(currentMillis);
        Serial.print("previousLightMillis");Serial.println(previousLightMillis);
        Serial.println("===========================");
        
        previousLightMillis=currentMillis;
    }
}


void ledShowColor(int colorIndex){
    if(colorIndex>=0&&colorIndex<=255){
        ledR=255-colorIndex;
        anologWrite(rLedPin,ledR);
    }else if(colorIndex>=256&&colorIndex<=511){
        ledR=colorIndex-256;
        anologWrite(rLedPin,ledR);
    }else if(colorIndex>=512&&colorIndex<=767){
        ledG=767-colorIndex;
        anologWrite(gLedPin,ledG);
    }else if(colorIndex>=768&&colorIndex<=1023){
        ledG=colorIndex-768;
        anologWrite(gLedPin,ledG);
    }else if(colorIndex>=1024&&colorIndex<=1279){
        ledB=1279-colorIndex;
        anologWrite(bLedPin,ledB);
    }else if(colorIndex>=1280&&colorIndex<=1535){
        ledB=colorIndex-1280;
        anologWrite(bLedPin,ledB);
    }
    delay(colorChangeDelay);
}

void fadeOn(){
    Serial.println("");
    Serial.println("Fade On");
    int i;
    while(i<255){
        i++;
        ledR++;
        ledG++;
        ledB++;
        anologWrite(rLedPin,ledR);
        anologWrite(gLedPin,ledG);
        anologWrite(bLedPin,ledB);
    }
}

void fadeOff(){
    while(ledR>0){
        ledR--;
        anologWrite(rLedPin,ledR);
    }
    while(ledG>0){
        ledG--;
        anologWrite(gLedPin,ledG);
    }
    while(ledB>0){
        ledB--;
        anologWrite(bLedPin,ledB);
    }    
    colorIndex=0;
}

🎯心得:

变量的声明其实可以更加详细一点,可以使用完整的英文来表示,而不是只使用单词的开头字母。因为在真正写代码的过程中,变量其实大多不是自己拼出来,而是直接复制粘贴,而在此时,准确知道变量的含义并复制来使用尤为重要。所以不要忌讳变量名过长能快速识别并且能拿来使用是更重要的

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值