package ovo.h

master
IoTgod 5 years ago
parent 6747ee21bd
commit 9bd8143b7d
  1. 77
      Smart-Car-on-ArduinoNano.ino
  2. 90
      ovo.h

@ -11,82 +11,12 @@
#include "Adafruit_NeoPixel.h" //彩色灯珠驱动
#include "comm.h" //传感器数据读取
#include "motor.h" //电机控制
#include "ovo.h"
#define PIN 4
#define NUMPIXELS 2
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setTimeout(auto function,const int delay)
{
static bool on = 1;
if(on == 1){
static unsigned long startTime = millis();
if(millis() - startTime > delay){
function();
on == 0;
}
}
}
void setInterval(auto function, const int delay)
{
static unsigned long startTime = millis();
if(millis() - startTime > delay){
function();
startTime = millis();
}
}
void setSwitch(auto function1, auto function2, const int delay1, const int delay2)
{
static unsigned long startTime = millis();
if(millis() - startTime < delay1){
function1();
}else if(millis() - startTime >= delay1 && millis() - startTime < delay1 + delay2){
function2();
}else if(millis() - startTime >= delay1 + delay2){
startTime = millis();
}
}
class slowWrite
{
public:
inline void set(int t_pin, unsigned int t_delay){
pin = t_pin;
delay = t_delay;
};
inline void high(){
startTime = millis();
state = 1;
};
inline void low(){
startTime = millis();
state = 0;
};
inline void run(){
if(state == 1 && millis() - startTime < delay){
analogWrite(pin, ((millis() - startTime) * 255 / delay));Serial.println(((millis() - startTime) * 255 / delay));
}else if(state == 0 && millis() - startTime < delay){
analogWrite(pin, 255-((millis() - startTime) * 255 / delay));Serial.println(255-((millis() - startTime) * 255 / delay));
}else{
state = -1;
}
};
private:
unsigned long startTime;
int state;
int pin;
unsigned int delay;
};
slowWrite m;
///////Test Version Only
@ -107,9 +37,8 @@ void loop() {
m.set(LED_BUILTIN, 2000);
m.run();
setTimeout([]{setInterval([]{m.high();}, 6000);},3000);
setInterval([]{m.low();}, 6000);
setTimeout([]{setInterval([]{m.high();}, 6000);},3000);
setInterval([]{m.low();}, 6000);
}

90
ovo.h

@ -0,0 +1,90 @@
/**
* ovo-arduino Main File
*
* @category ovo-arduino
* @package ovo.cpp
* @copyright Copyright (c) 2019 EEENeko (https://github.com/eeeneko)
* @license GNU General Public License 2.0
* @version 0.0.1
*/
#ifndef _OVO_H
#define _OVO_H
void setTimeout(auto function,const int delay)
{
static bool on = 1;
if(on == 1){
static unsigned long startTime = millis();
if(millis() - startTime > delay){
function();
on == 0;
}
}
}
void setInterval(auto function, const int delay)
{
static unsigned long startTime = millis();
if(millis() - startTime > delay){
function();
startTime = millis();
}
}
void setSwitch(auto function1, auto function2, const int delay1, const int delay2)
{
static unsigned long startTime = millis();
if(millis() - startTime < delay1){
function1();
}else if(millis() - startTime >= delay1 && millis() - startTime < delay1 + delay2){
function2();
}else if(millis() - startTime >= delay1 + delay2){
startTime = millis();
}
}
class slowWrite
{
public:
inline void set(int t_pin, unsigned int t_delay){
pin = t_pin;
delay = t_delay;
};
inline void high(){
startTime = millis();
state = 1;
};
inline void low(){
startTime = millis();
state = 0;
};
inline void run(){
if(state == 1 && millis() - startTime < delay){
analogWrite(pin, ((millis() - startTime) * 255 / delay));Serial.println(((millis() - startTime) * 255 / delay));
}else if(state == 0 && millis() - startTime < delay){
analogWrite(pin, 255-((millis() - startTime) * 255 / delay));Serial.println(255-((millis() - startTime) * 255 / delay));
}else{
state = -1;
}
};
private:
unsigned long startTime;
int state;
int pin;
unsigned int delay;
};
#endif
Loading…
Cancel
Save