cocoakang +

初版

电池盒终于到啦。 考虑到LED和arduino都需要供电,本想着用转接头转换出来分路供电,结果买时没看清,收到的转换器和电池盒都是公头。由于uno只有一个5V恒压输出,而L298N的功率输入和逻辑输入都需要,单独为这一个口配一个面包板又麻烦,真是男上加男。(原计划逻辑电压由arduino板上输出,功率输入由电源直接给出) 反正之后会画一个电路板,这里就先将就下,把这两个5V共用下。 电路如下: Image text

手忙脚乱做了个底座: Image text

一个变亮的gif: Image text

代码很简单,放在这里了

#include <DS3231_Simple.h>

//DS3231
DS3231_Simple Clock;

//LED&L298N
int in1 = 3;
int in2 = 4;
int ena = 5;
bool output_dir = true;
float LED_current_power = 0.0f;
float MAX_LIGHTNESS = 256.0f;

float adjust_time = 180;//5*60;//seconds
float hold_on_time = 120;//5*60;//seconds
float adjust_step = MAX_LIGHTNESS / adjust_time*0.001;

//alarm logic
#define       GLOBAL_STATE_IDLE         0
#define       GLOBAL_STATE_WAKING       1
#define       GLOBAL_STATE_MAX_LIGHT    2
#define       GLOBAL_STATE_ENDING       3

int ALARM_Hour         =         07;//6;
int ALARM_Minute       =         00;//30;
int ALARM_Second       =         0;

int global_state = GLOBAL_STATE_IDLE;

void int_func()
{
  Serial.println("int0");
  global_state = GLOBAL_STATE_WAKING;
  LED_current_power = 0.0;
  set_led_brightness();
  Serial.println("int1");
}

void set_led_brightness()
{
  unsigned char brightness = min(max(int(LED_current_power),0),255);
  analogWrite(ena,brightness);
}

void setup() {
  //init 3231
  attachInterrupt(0, int_func, RISING);//当int.0(uno的引脚为2)电平改变时,触发中断函数int_func
  
  Clock.begin();
  Clock.disableAlarms();
  DateTime MyTimestamp = Clock.read();   
  MyTimestamp.Hour   = ALARM_Hour;    
  MyTimestamp.Minute = ALARM_Minute;
  MyTimestamp.Second = ALARM_Second;    
  Clock.setAlarm(MyTimestamp, DS3231_Simple::ALARM_MATCH_MINUTE_HOUR);  
  
  //init LED & 298N  
  pinMode(in1,OUTPUT);
  pinMode(in2,OUTPUT);
  pinMode(ena,OUTPUT);  
  digitalWrite(in1,output_dir);
  digitalWrite(in2,!output_dir);
  analogWrite(ena,0);
  LED_current_power = 0.0f;

  //global_state
  global_state = GLOBAL_STATE_IDLE;
  
  //debug serial
  Serial.begin(9600);
  Serial.println("init done");
}

void loop() {
//  Clock.printTimeTo_HMS(Serial);
//  Serial.println();
//  
//  delay(1000);
//  
  switch(global_state){
    case GLOBAL_STATE_IDLE:
    break;
    case GLOBAL_STATE_WAKING://turn on the light slowly
      Serial.println("state waking");
      while(LED_current_power < MAX_LIGHTNESS){
        LED_current_power+= adjust_step;
        set_led_brightness();
        delay(1);//delay 1ms
      }
      LED_current_power = MAX_LIGHTNESS;
      global_state = GLOBAL_STATE_MAX_LIGHT;
    break;
    case GLOBAL_STATE_MAX_LIGHT://hold max light for 5 mininutes
      Serial.println("state max light");
      set_led_brightness();
      delay(hold_on_time*1000);
      global_state = GLOBAL_STATE_ENDING;
    break;
    case GLOBAL_STATE_ENDING://turn down the light slowly
      Serial.println("state ending");
      while(LED_current_power > 0){
          LED_current_power-= adjust_step;
          set_led_brightness();
          delay(1);//delay 1ms
        }
        LED_current_power = 0;
        global_state = GLOBAL_STATE_IDLE;
    break;
    default:
      global_state = GLOBAL_STATE_IDLE;
    break;
  }
}

库用的是这个,相当好用,感谢!

Blog

Photo

Project