I'm having the mentioned in the title compilation error and this is not the first time I encounter it and I don't know how to fix it, what should I do?
#include <Servo.h>
class buttonType {
private:
byte Pin;
byte State = HIGH;
byte lastState = HIGH;
unsigned long lastChange = 0;
unsigned long lastLow = 0;
unsigned long pressTime = 0;
public:
void init(byte pinNo){Pin = pinNo; pinMode(Pin, INPUT_PULLUP);}
boolean released();
unsigned long getPressTime() {return pressTime;}
};
boolean buttonType::released(){
byte actState = digitalRead(Pin);
if (actState != lastState){
lastChange = millis();
lastState = actState;
}
if (State != lastState && millis()-lastChange > 30){
State = lastState;
if (State == HIGH) {
pressTime = millis()-lastLow;
return true;
} else {
lastLow = millis();
}
}
return false;
}
enum Mode {SetTime, Flying, Determalisated};
int TargetHeading;
int CurrentHeading = 90;
int Deviation;
int Variation;
int setFlightTime;
int Counter = 0;
constexpr int maxCounter = 6;
constexpr byte buttonPin = 10;
buttonType button;
bool shortPress = false;
bool longPress = false;
Servo EP;
unsigned long FlyTime;
void Button(){
if (button.released()){
if (button.getPressTime() < 200){
shortPress = true;
} else {
longPress = true;
}
}
}
void Timer() {
Counter++;
if (Counter > maxCounter) {
Counter = 1;
}
}
void AcquireTargetHeading() {
TargetHeading = analogRead(A0);
TargetHeading = map(TargetHeading, 0, 1023, 0, 360);
Serial.println(TargetHeading);
}
void GuidanceSystem() {
Deviation = TargetHeading - CurrentHeading;
Variation = 90 + Deviation;
constrain(Variation, 80, 110);
EP.write(Variation);
if (millis() - FlyTime > setFlightTime * 60000UL) {
EP.write(90);
Mode(Determalisated);
}
else if (longPress) {
EP.write(90);
Mode(Determalisated);
}
}
void Modes() {
switch(Mode) {
case SetTime:
if (shortPress) {
Timer();
}
if (longPress) {
setFlightTime = Counter;
FlyTime = millis();
Mode(Flying);
}
break;
case Flying:
AcquireTargetHeading();
GuidanceSystem();
break;
case Determalisated:
if (longPress) {
Mode(SetTime);
}
break;
}
}
void setup() {
Serial.begin(9600);
pinMode(A0, OUTPUT);
EP.attach(9);
}
void loop() {
Button();
Modes();
}
Error message:
C:\Users\Cristy\Desktop\Ceas_electronic_F1E_1_buton_2\Ceas_electronic_F1E_1_buton_2.ino: In function 'void Modes()':
C:\Users\Cristy\Desktop\Ceas_electronic_F1E_1_buton_2\Ceas_electronic_F1E_1_buton_2.ino:102:14: error: expected primary-expression before ')' token
switch(Mode) {
^
Multiple libraries were found for "Servo.h"
Used: C:\Users\Cristy\Documents\Arduino\libraries\Servo
Not used: C:\Users\Cristy\AppData\Local\Arduino15\libraries\Servo
exit status 1
Compilation error: expected primary-expression before ')' token
Your code calls a function named Mode() that does not exist in the sketch
Mode(Determalisated);
but you do have an enum of that name, hence the error
How do I correct that?Isn't 'Mode(Determalisated)' supposed to switch to case 'Determalisated'?
Your switch/case uses the Mode variable to determine which case, if any, to execute. However, you don't have a variable named Mode, you have an enum of that name
Add a new variable named currentMode of data type byte, use that as the switch variable and when you want to change modes do
currentMode = SetTime;
or whatever mode you want to change to
Sorry for the reply, it's a long time since I checked, but I still don't understand I tried to use currentMode as a byte but it doesn't work, can you show me how the code should look?
I have taken your code and added a new global variable named currentState. You should be able to guess what its purpose is from its name. I have also made some other changes.
The sketch compiles but I have no idea whether it does what you want or even what it is supposed to do. Where did you get the skerch from as I am certain that you did not write it
#include <Servo.h>
class buttonType
{
private:
byte Pin;
byte State = HIGH;
byte lastState = HIGH;
unsigned long lastChange = 0;
unsigned long lastLow = 0;
unsigned long pressTime = 0;
public:
void init(byte pinNo)
{
Pin = pinNo;
pinMode(Pin, INPUT_PULLUP);
}
boolean released();
unsigned long getPressTime()
{
return pressTime;
}
};
boolean buttonType::released()
{
byte actState = digitalRead(Pin);
if (actState != lastState)
{
lastChange = millis();
lastState = actState;
}
if (State != lastState && millis() - lastChange > 30)
{
State = lastState;
if (State == HIGH)
{
pressTime = millis() - lastLow;
return true;
}
else
{
lastLow = millis();
}
}
return false;
}
enum Mode
{
SetTime,
Flying,
Determalisated
};
Mode currentMode = SetTime;
int TargetHeading;
int CurrentHeading = 90;
int Deviation;
int Variation;
int setFlightTime;
int Counter = 0;
constexpr int maxCounter = 6;
constexpr byte buttonPin = 10;
buttonType button;
bool shortPress = false;
bool longPress = false;
Servo EP;
unsigned long FlyTime;
void Button()
{
if (button.released())
{
if (button.getPressTime() < 200)
{
shortPress = true;
}
else
{
longPress = true;
}
}
}
void Timer()
{
Counter++;
if (Counter > maxCounter)
{
Counter = 1;
}
}
void AcquireTargetHeading()
{
TargetHeading = analogRead(A0);
TargetHeading = map(TargetHeading, 0, 1023, 0, 360);
Serial.println(TargetHeading);
}
void GuidanceSystem()
{
Deviation = TargetHeading - CurrentHeading;
Variation = 90 + Deviation;
constrain(Variation, 80, 110);
EP.write(Variation);
if (millis() - FlyTime > setFlightTime * 60000UL)
{
EP.write(90);
currentMode = Determalisated;
}
else if (longPress)
{
EP.write(90);
currentMode = Determalisated;
}
}
void Modes()
{
switch (currentMode)
{
case SetTime:
if (shortPress)
{
Timer();
}
if (longPress)
{
setFlightTime = Counter;
FlyTime = millis();
currentMode = Flying;
}
break;
case Flying:
AcquireTargetHeading();
GuidanceSystem();
break;
case Determalisated:
if (longPress)
{
currentMode = SetTime;
}
break;
}
}
void setup()
{
Serial.begin(9600);
pinMode(A0, OUTPUT);
EP.attach(9);
}
void loop()
{
Button();
Modes();
}
The thing with the button I took from another project that I found, that's the only thing I took from somewhere else...
What is the sketch meant to do ?
Around the same thing but it includes another servo that after the set time it just swings 90 degrees. I tried the sketch in Wokwi but the Counter stays to 0, no short press is detected either a long one .Should I give you the link to the other sketch?
Is the other sketch that you are referring to in another topic ?