I'm getting this message for each of my void functions. Code below
//----------------------------------------------------------------------------------------------
#include <Arduino.h>
#define IN1 7
#define IN2 8
#define ENA 10
#define Bttn 2
#define Sfty 1
volatile int button=0;
volatile int safety=0;
volatile int PWMspeed=0;
int accel_time=1000;
int const_time=3000;
int open_time=5000;
int maxspeed=255;
int minspeed=50;
int steps=20;
int interval=accel_time/steps;
int increment=(maxspeed-minspeed)/steps;
unsigned long t0=0;
int debounce=50;
void motorcontrol();
void CW();
void CCW();
void OFF();
void setup() {
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(ENA,OUTPUT);
pinMode(Bttn,INPUT_PULLUP);
pinMode(Sfty,INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
motorcontrol();
}
void motorcontrol() {
enum states {
IDLE,
CONSTANT
};
states state;
button=digitalRead(Bttn);
safety=digitalRead(Sfty);
switch (state) {
case IDLE:
Serial.println("IDLE ");
unsigned static long t1=millis();
if(button==HIGH) {
state=CONSTANT;
}
break;
case CONSTANT:
Serial.println("CONSTANT ");
CW();
analogWrite(ENA,maxspeed);
if(millis()-t1<=const_time) {
Serial.println(millis()-t1);
if (safety==HIGH) {
OFF();
state=IDLE;
}
}else{
OFF();
state=IDLE;
}
break;
default:
Serial.println("Default Case Not Reached");
}
}
void CW() {
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
}
void CCW() {
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
}
void OFF() {
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
analogWrite(ENA,0);
}
It compiles fine for me.
There are some problems though.
ederbm22:
switch (state) {
state is not initialised here.
You are comparing a signed integer with an unsigned integer.
It would be a good idea to properly format your code...
In IDE press ctrl-t.
Why are the above declared with the volatile keyword?
On many Arduino boards (including UNO and Nano) pins 0 and 1 are used for the serial interface. You should probably not use them.
Change:
int const_time = 3000;
to:
unsigned const_time = 3000;
Change:
states state;
to:
static states state = IDLE;
Thanks for the help. I'm still very new to Arduino programming. However, I'm still getting the same error.
updated code
#include <Arduino.h>
#define IN1 7
#define IN2 8
#define ENA 10
#define Bttn 3
#define Sfty 2
int button = 0;
int safety = 0;
int PWMspeed = 0;
unsigned accel_time = 1000;
unsigned const_time = 3000;
unsigned open_time = 5000;
int maxspeed = 255;
int minspeed = 50;
int steps = 20;
unsigned interval = accel_time / steps;
int increment = (maxspeed - minspeed) / steps;
unsigned debounce = 50;
void motorcontrol();
void CW();
void CCW();
void OFF();
enum states {
IDLE,
ACCEL_CW,
CONSTANT_CW,
DECEL_CW,
OPEN
};
static states state = IDLE;
void setup() {
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(Bttn, INPUT_PULLUP);
pinMode(Sfty, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
motorcontrol();
}
void motorcontrol() {
button = digitalRead(Bttn);
safety = digitalRead(Sfty);
switch (state) {
case IDLE:
Serial.println("IDLE ");
if (button == HIGH) {
state = ACCEL_CW;
}
break;
case ACCEL_CW:
Serial.println("ACCEL ") unsigned static long t2 = millis();
if (millis() - t2 = accel_time) {
CW();
unsigned static long t0 = millis();
if (millis() - t0 <= interval) {
PWMspeed += increment;
if (safety == HIGH) {
OFF();
state = IDLE;
}
}
} else {
state = CONSTANT_CW;
}
break;
case CONSTANT_CW:
unsigned static long t1 = millis();
Serial.println("CONSTANT ");
CW();
analogWrite(ENA, maxspeed);
if (millis() - t1 <= const_time) {
if (safety == HIGH) {
OFF();
state = IDLE;
}
} else {
state = DECEL_CW;
}
break;
case DECEL_CW:
Serial.println("DECEL ") unsigned static long t3 = millis();
if (millis() - t3 = accel_time) {
CW();
unsigned static long t4 = millis();
if (millis() - t4 <= interval) {
PWMspeed -= increment;
if (safety == HIGH) {
OFF();
state = IDLE;
}
}
} else {
state = OPEN;
}
break;
case OPEN:
unsigned static long t5 = millis();
if (millis() - t5 <= open_time) {
OFF();
}
state = IDLE;
break;
default:
Serial.println("Default Case Not Reached");
}
}
void CW() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
}
void CCW() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
}
void OFF() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
analogWrite(ENA, 0);
}
You need to add a ; after ACCEL")
Please add the full error message. It gives a line number and character number where the error occurred.
Usually your problem is before that.
Often the complaint about function definition not allowed before { token is caused by a missing or extraneous} in your code before that. But I cannot find that here.
Tip: if in IDE you hover over a }, the matching { will light up. This can make it easier to find a mismatch.
check for = vs == also
and a ; after DECEL")
if (millis() - t2 = accel_time) { // = or ==?
if (millis() - t3 = accel_time) {
system
Closed
August 6, 2023, 3:10pm
11
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.