Sorry if some of my source looks really bad. This is my first attempt writing Arduino code as well as c++. I usually stay in my comfort zone of c#.
I'm getting the following errors when trying to run my source on a Tinkercad circuit. The way Tinkercad spits out errors is horrible for learning. Anyone who might be able to point out my mistakes that it is having a fit with would be very helpful.
Errors
- 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
My source code
/* 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;
}
}
}
};
An example of the code on Tinkercad Circuits can be found at... https://www.tinkercad.com/things/gmGeFVKOA3e-adrunio-test/editel
Once on the page, click the "Simulate" play button (bottom left) and then hit the "Start Simulation" button at the top. You should see the same errors I'm seeing.