对不起,如果我的一些来源看起来真的很糟糕。这是我第一次尝试编写 Arduino 代码以及 c++。我通常呆在 c# 的舒适区。
尝试在 Tinkercad 电路上运行我的源代码时出现以下错误。Tinkercad 吐出错误的方式对于学习来说是可怕的。任何能够指出我的错误的人都会非常有帮助。
错误
- 2:23: error: use of enum 'LedActionTypes' without previous
declaration
- 4:23: error: use of enum 'SignalTypes' without previous declaration
- 4:64: error: use of enum 'SignalDirections' without previous declaration
- 5:23: error: use of enum 'SignalTypes' without previous declaration
- 5:64: error: use of enum 'SignalDirections' without previous declaration
- 5:103: error: use of enum 'DigitalSignalValues' without previous declaration
- 8:16: error: variable or field 'AddAction' declared void
- 8:16: error: 'LedAction' was not declared in this scope
我的源代码
/* Enums */
enum SignalTypes
{
General = 0,
Analog = 1,
Digital = 2,
};
enum SignalDirections
{
Inbound = 0x0,
Outbound = 0x1,
};
enum DigitalSignalValues
{
Low = 0x0,
High = 0x1,
};
enum LedActionTypes
{
None = 0,
ChangeBrightness = 1,
};
/* LED Action object */
class LedAction {
// Functions
void Constructor(enum LedActionTypes action, int intensity, int delay)
{
// Check for valid intensity value
if (intensity < 0)
intensity = 0;
if (intensity > 255)
intensity = 255;
Intensity = intensity;
Finished = false;
Action = action;
CompleteOn = millis() + delay;
}
public:
// Properties
boolean Finished;
enum LedActionTypes Action = None;
int Intensity = 0;
unsigned long CompleteOn = 0;
// Constructor
LedAction()
{
Constructor(Action, Intensity, 0);
}
LedAction(enum LedActionTypes action)
{
Constructor(action, Intensity, 0);
}
LedAction(enum LedActionTypes action, int intensity, int delay)
{
Constructor(action, intensity, delay);
}
// Methods
void Loop() {
if (Finished) { return; }
unsigned long currentTimeStamp = millis();
if (CompleteOn >= currentTimeStamp)
{
//Process the action
Finished = true;
}
}
};
/* LED object */
class Led {
// Functions
void Constructor(enum SignalTypes signalType, byte pbPin, enum SignalDirections signalDirection, int intensity)
{
// Check for valid intensity value
if (intensity < 0)
intensity = 0;
if (intensity > 255)
intensity = 255;
Intensity = intensity;
Constructor(SignalType, PBPin, SignalDirection, DigitalSignalValue);
}
void Constructor(enum SignalTypes signalType, byte pbPin, enum SignalDirections signalDirection, enum DigitalSignalValues signalValue)
{
SignalType = signalType;
PBPin = pbPin;
SignalDirection = signalDirection;
DigitalSignalValue = signalValue;
}
public:
// Properties
byte PBPin;
int Intensity;
enum DigitalSignalValues DigitalSignalValue;
enum SignalTypes SignalType = Analog;
enum SignalDirections SignalDirection = Outbound;
LedAction Actions[20]{ LedAction(None) };
// Constructor
Led()
{
Constructor(SignalType, 0, SignalDirection, 0);
}
Led(byte pbPin, enum SignalDirections signalDirection, int intensity)
{
Constructor(SignalType, pbPin, signalDirection, intensity);
}
Led(byte pbPin, enum SignalDirections signalDirection, enum DigitalSignalValues signalValue)
{
Constructor(SignalType, pbPin, signalDirection, signalValue);
}
Led(enum SignalTypes signalType, byte pbPin, enum SignalDirections signalDirection, int intensity)
{
Constructor(signalType, pbPin, signalDirection, intensity);
}
Led(enum SignalTypes signalType, byte pbPin, enum SignalDirections signalDirection, enum DigitalSignalValues signalValue)
{
Constructor(signalType, pbPin, signalDirection, signalValue);
}
// Methods
void Setup()
{
switch (SignalType)
{
case Analog:
analogWrite(PBPin, Intensity);
break;
case Digital:
digitalWrite(PBPin, Intensity);
pinMode(PBPin, SignalDirection);
break;
}
}
void Loop()
{
int n;
// Loop through all actions and find unfinished ones then fire them off
for ( n=0 ; n<20 ; ++n )
{
if (!Actions[n].Finished)
{
Actions[n].Loop();
if (Actions[n].Finished)
{
//Action was just flagged ready to process
switch (Actions[n].Action)
{
case ChangeBrightness:
digitalWrite(PBPin, Actions[n].Intensity);
break;
}
}
}
}
}
void AddAction(LedAction action)
{
int n;
// Loop through all actions and find an unfinished one then reuse it
for (n = 0; n < 20; ++n)
{
if (!Actions[n].Finished)
{
action.Finished = false;
Actions[n] = action;
break;
}
}
}
void ClearAllActions()
{
int n;
// Loop through all actions and mark all as finished
for (n = 0; n < 20; ++n)
{
if (!Actions[n].Finished)
{
Actions[n].Finished = true;
}
}
}
};
Tinkercad 电路上的代码示例可以在... https://www.tinkercad.com/things/gmGeFVKOA3e-adrunio-test/editel找到
进入页面后,单击“模拟”播放按钮(左下角),然后单击顶部的“开始模拟”按钮。您应该看到与我看到的相同的错误。