我有一个程序应该分4个步骤同时打开4个LED:
程序使stpe 1和2然后停止wotking并且LED保持在250mv。
我尝试更改前两个步骤的持续时间,但在前两个步骤完成后它总是停止,并且不会继续3和4。
我使用面向对象编程,所以请耐心等待代码;实际上最重要的课程是效果和运动。
Node.cpp:此类创建一个节点,该节点由所选的振幅和持续时间定义,例如:幅度:10mv持续时间:500ms
#include "Arduino.h"
#include "Node.h"
Node::Node(){}
Node::~Node(){}
int Node::getAmplitude()
{
return _amplitude;
}
unsigned long Node::getDuration()
{
return _duration;
}
void Node::setPatternNode(int amplitude,unsigned long duration)
{
_amplitude=amplitude;
_duration=duration;
}
Pattern.cpp: 此类创建一个最初创建的节点数组,以产生特定的振动模式,例如:幅度{10,250,50,0}持续时间{500,500,500,500}
#include "Arduino.h"
#include "Pattern.h"
#include "Node.h"
#include "QueueArray.h"
#include "Motor.h"
Pattern::Pattern(){}
Pattern::~Pattern(){}
QueueArray <Node> Pattern::setPattern(int amplitude[4],unsigned long duration[4]){
QueueArray <Node> queue;
unsigned long nodeDurationSum = 0;
for (int i=0;i<4;i++){
Node node;
nodeDurationSum = nodeDurationSum + duration[i]; //the reference for duration calculation is patternStartTime
node.setPatternNode(amplitude[i],nodeDurationSum);
queue.enqueue(node);
}
return queue;
}
Motor.cpp:此类影响由类Pattern创建的模式到选定的电机。
#include "Arduino.h"
#include "Motor.h"
#include "QueueArray.h"
#include "Node.h"
#include "Pattern.h"
Motor::Motor()
{
}
Motor::Motor(int pin)
{
pinMode(pin, OUTPUT);
_pin = pin;
}
Motor::~Motor()
{
}
void Motor::runMotor()
{
if(_isMotorActive){
_currentTime=millis();//_currentTime is declared as "unsigned long"
Serial.println(_currentTime);
if(_currentTime < _currentNode.getDuration())
{
analogWrite(_pin,_currentNode.getAmplitude());
}
else
{
if(_currentPattern.isEmpty())
{
_isMotorActive = false;
}
updateCurrentNode();
}
}
}
void Motor::motorSetPattern(QueueArray <Node> p)
{
_currentPattern = p;
_isMotorActive = true;
_patternStartTime = millis();//_patternStartTime is declared as "unsigned long"
updateCurrentNode();
}
int Motor::getCurrentPatternLength()
{
return _currentPattern.count();
}
void Motor::updateCurrentNode()
{
_currentNode = _currentPattern.dequeue();
}
Effect.cpp:此类运行多于1个电机,每个电机都按照受影响的模式运行。
#include "Arduino.h"
#include "Effect.h"
#include "Motor.h"
Effect::Effect(){}
Effect::Effect(Motor m1,Motor m2,Motor m3,Motor m4,QueueArray <Node> p1,QueueArray <Node> p2,QueueArray <Node> p3,QueueArray <Node> p4)
{
_vibmotor1=m1;
_vibmotor2=m2;
_vibmotor3=m3;
_vibmotor4=m4;
_motorPattern1 = p1;
_motorPattern2 = p2;
_motorPattern3 = p3;
_motorPattern4 = p4;
_vibmotor1.motorSetPattern(_motorPattern1);
_vibmotor2.motorSetPattern(_motorPattern2);
_vibmotor3.motorSetPattern(_motorPattern3);
_vibmotor4.motorSetPattern(_motorPattern4);
}
Effect::~Effect()
{
}
void Effect::runEffect()
{
_vibmotor1.runMotor();
_vibmotor2.runMotor();
_vibmotor3.runMotor();
_vibmotor4.runMotor();
}
这是arduino草图:
#include <Effect.h>
#include <Motor.h>
#include <Node.h>
#include <Pattern.h>
#include <QueueArray.h>
Motor vibmotor1(3);
Motor vibmotor2(5);
Motor vibmotor3(6);
Motor vibmotor4(9);
Pattern pattern;
int amplitude1[4] = {10,250,50,0};
unsigned long duration1[4] = {500,500,500,500};
int amplitude2[4] = {10,250,50,0};
unsigned long duration2[4] = {500,500,500,500};
int amplitude3[4] = {10,250,50,0};
unsigned long duration3[4] = {500,500,500,500};
int amplitude4[4] = {10,250,50,0};
unsigned long duration4[4] = {500,500,500,500};
QueueArray <Node> p1 = pattern.setPattern(amplitude1,duration1);
QueueArray <Node> p2 = pattern.setPattern(amplitude2,duration2);
QueueArray <Node> p3 = pattern.setPattern(amplitude3,duration3);
QueueArray <Node> p4 = pattern.setPattern(amplitude4,duration4);
Effect effect1(vibmotor1,vibmotor2,vibmotor3,vibmotor4,p1,p2,p3,p4);
void setup()
{
Serial.begin(9600);
}
void loop()
{
effect1.runEffect();
}
答案 0 :(得分:3)
在void Motor::run()
中
您正在使用_currentTime
填充millis()
但是它给了你自arduino开始以来的时间,而不是从Effect::runEffect()
开始。所以它可能已经高于你第一次效果的持续时间了。
首次致电runEffect()
时,请尝试存储实际的开始时间,并将其用作_currentTime
例如,在runMotor()
void Motor::runMotor()
{
if(_firstRun == true)
{
_startTime = millis();
_firstRun = false;
}
...your code...
_currentTime = millis() - _startTime;
...your code...
}
这就是我注意到的。先尝试一下。如果我没有帮助回来说出来。