I am trying to define a macro to replace the Delay(?)
#define MYWAIT(a) if(millis() > (millisPower + #a)); {}
// called as MYWAIT(100);
and it seems somehow to give an error - can anyone show me where I have gone wrong with this.
Many thanks
I am trying to define a macro to replace the Delay(?)
#define MYWAIT(a) if(millis() > (millisPower + #a)); {}
// called as MYWAIT(100);
and it seems somehow to give an error - can anyone show me where I have gone wrong with this.
Many thanks
Post the whole code. Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.
Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.
/*
BLUE TOOTH Throttle application for model railway
One train driver
Courtesy of URB - Steve Massikker - https://arduinorailwaycontrol.com/
Arduino - all the Elves
and me.
Resources : https://create.arduino.cc/projecthub/ryanchan/how-to-use-the-l298n-motor-driver-b124c5
https://www.14core.com/wiring-driving-the-l298n-h-bridge-on-2-to-4-dc-motors/
https://www.instructables.com/Arduino-2-in-1-Model-Train-Controller/
Dejan Nedelkovski, www.HowToMechatronics.com
UNO R3 or Nano v3 with CH340 chip
L298N Motor Driver
HC-05 BLUETOOTH
Version 1.0 -
TODO:
WISH:
*/
//---------------------------------------------------------------
// PINS FOR MOTOR DRIVER -A
#define ENA_PIN 5 // PWM
#define IN1_PIN 7
#define IN2_PIN 8
// LED
#define ledA A0
// MACROS
#define PRINT_A(a) Serial.println("HELLO:" #a".");
#define PRINT_B(b) Serial.println("WE ARE CONNECTED: "#b".");
#define PRINT_C(c) Serial.println("GOING: "#c".");
#define PRINT_D(d) Serial.println("STOPPED: "#d".");
#define PRINT_E(e) Serial.println("SPEED: "#e".");
#define MYWAIT(a) if(millis() > (millisPower + #a)); {} // called as MYWAIT(100);
// SPEED 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
byte speedArray [] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 255};
byte speedTrain = 0;
// USED WITHIN
int counter = 0;
String inputString = "";
static unsigned long millisPower;
bool stringComplete = false;
/////////////////////////// SETUP
void setup () {
// SET THE SERIAL MONITOR
Serial.begin(38400); // Default communication rate of the Bluetooth module
inputString.reserve(4);
Serial.print ("File: "); Serial.println(__FILE__);
Serial.print (" Date: "); Serial.println(__DATE__);
Serial.println("------------------------------------------ - ");aa
// INITIALIZE THE MOTOR DRIVER (A)
pinMode(ENA_PIN, OUTPUT);
pinMode(IN1_PIN, OUTPUT);
pinMode(IN2_PIN, OUTPUT);
// INITIALIZE THE LED
pinMode (ledA, OUTPUT);
// SET THE DEFULT CONDITION
analogWrite(ENA_PIN, 0);
digitalWrite(IN1_PIN, LOW);
digitalWrite(IN2_PIN, LOW);
// SET THE VARIABLES
millisPower = millis();
// BLINK THE LED
for (int x = 0; x < 3; x++)
{
digitalWrite(ledA, HIGH);
if (millis() > (millisPower + 2500)) {}
digitalWrite(ledA, LOW);
if (millis() > (millisPower + 2500)) {}
}
}
/////////////////////////// LOOP //////////////////////////////////
void loop ()
{
if (Serial.available() > 0)
{ // Checks whether data is comming from the serial port
inputString = Serial.read(); // Reads the data from the serial port
}
// STOPPED
if (inputString.charAt(0) == 'STP') {
PRINT_D(PLAYER);
analogWrite(ENA_PIN, 0);
counter = 0;
}
// DIRECTION
if (inputString.charAt(0) == 'FWD') {
PRINT_C(FORWARD);
digitalWrite(IN1_PIN, LOW);
digitalWrite(IN2_PIN, HIGH);
}
// DIRECTION
if (inputString.charAt(0) == 'REV') {
PRINT_C(REVERSE);
digitalWrite(IN1_PIN, HIGH);
digitalWrite(IN2_PIN, LOW);
}
MYWAIT(10);
// SPEED
if (inputString.charAt(0) == '*') {
PRINT_E(0) ;
counter = inputString.charAt(1);
speedTrain = speedArray[counter];
analogWrite(ENA_PIN, speedTrain);
}
}
//////////////////////////// FUNCTIONS /////////////////////////////
//-------------------------------------------------------------
//-------------------------------------------------------------
The error messages are :
`Arduino: 1.8.16 (Windows Store 1.8.51.0) (Windows 10), Board: "Arduino Uno"
C:\Users\ann\Downloads\Documents\Arduino\Throttle_Blue_1\Throttle_BLUE_1\Throttle_BLUE_1.ino:111:32: warning: character constant too long for its type
if (inputString.charAt(0) == 'STP') {
^~~~~
C:\Users\ann\Downloads\Documents\Arduino\Throttle_Blue_1\Throttle_BLUE_1\Throttle_BLUE_1.ino:118:32: warning: character constant too long for its type
if (inputString.charAt(0) == 'FWD') {
^~~~~
C:\Users\ann\Downloads\Documents\Arduino\Throttle_Blue_1\Throttle_BLUE_1\Throttle_BLUE_1.ino:125:32: warning: character constant too long for its type
if (inputString.charAt(0) == 'REV') {
^~~~~
C:\Users\ann\Downloads\Documents\Arduino\Throttle_Blue_1\Throttle_BLUE_1\Throttle_BLUE_1.ino: In function 'void setup()':
Throttle_BLUE_1:68:67: error: 'aa' was not declared in this scope
Serial.println("------------------------------------------ - ");aa
^~
C:\Users\ann\Downloads\Documents\Arduino\Throttle_Blue_1\Throttle_BLUE_1\Throttle_BLUE_1.ino: In function 'void loop()':
C:\Users\ann\Downloads\Documents\Arduino\Throttle_Blue_1\Throttle_BLUE_1\Throttle_BLUE_1.ino:46:51: warning: ISO C++ forbids comparison between pointer and integer [-fpermissive]
#define MYWAIT(a) if(millis() > (millisPower + #a)); {} // called as MYWAIT(100);
^
C:\Users\ann\Downloads\Documents\Arduino\Throttle_Blue_1\Throttle_BLUE_1\Throttle_BLUE_1.ino:131:3: note: in expansion of macro 'MYWAIT'
MYWAIT(10);
^~~~~~
exit status 1
'aa' was not declared in this scope
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
``
It's not obvious what your goal is. Are you trying to create a non block delay?? (this would be a programming oxymoron).
Doesn't '#' turn the macro argument into a string? Just leave it off since you want (millisPower + 100)
and not of (millisPower + "100")
.
This is also a concern.
Multicharacter constants are allowed, but they can't be longer than an int, so on the AVRs, that's just two characters.
Thank you John that solved this issue.
Thanks for this info - it also helped me understand macros.
This is what I think you were going for. Sadly, none of the 'if' statements apparently intended to cause delays (macro or otherwise) will cause any delay.
/*
BLUE TOOTH Throttle application for model railway
One train driver
Courtesy of URB - Steve Massikker - https://arduinorailwaycontrol.com/
Arduino - all the Elves
and me.
Resources : https://create.arduino.cc/projecthub/ryanchan/how-to-use-the-l298n-motor-driver-b124c5
https://www.14core.com/wiring-driving-the-l298n-h-bridge-on-2-to-4-dc-motors/
https://www.instructables.com/Arduino-2-in-1-Model-Train-Controller/
Dejan Nedelkovski, www.HowToMechatronics.com
UNO R3 or Nano v3 with CH340 chip
L298N Motor Driver
HC-05 BLUETOOTH
Version 1.0 -
TODO:
WISH:
*/
//---------------------------------------------------------------
// PINS FOR MOTOR DRIVER -A
#define ENA_PIN 5 // PWM
#define IN1_PIN 7
#define IN2_PIN 8
// LED
#define ledA A0
// MACROS
#define PRINT_A(a) Serial.println("HELLO:" #a".");
#define PRINT_B(b) Serial.println("WE ARE CONNECTED: "#b".");
#define PRINT_C(c) Serial.println("GOING: "#c".");
#define PRINT_D(d) Serial.println("STOPPED: "#d".");
#define PRINT_E(e) Serial.println("SPEED: "#e".");
// Changed '#a' to 'a' and removed ';'
#define MYWAIT(a) if(millis() > (millisPower + a)) {} // called as MYWAIT(100);
// SPEED 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
byte speedArray [] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 255};
byte speedTrain = 0;
// USED WITHIN
int counter = 0;
String inputString = "";
static unsigned long millisPower;
bool stringComplete = false;
/////////////////////////// SETUP
void setup ()
{
// SET THE SERIAL MONITOR
Serial.begin(38400); // Default communication rate of the Bluetooth module
inputString.reserve(4);
Serial.print ("File: "); Serial.println(__FILE__);
Serial.print (" Date: "); Serial.println(__DATE__);
Serial.println("------------------------------------------ - ");
// INITIALIZE THE MOTOR DRIVER (A)
pinMode(ENA_PIN, OUTPUT);
pinMode(IN1_PIN, OUTPUT);
pinMode(IN2_PIN, OUTPUT);
// INITIALIZE THE LED
pinMode (ledA, OUTPUT);
// SET THE DEFULT CONDITION
analogWrite(ENA_PIN, 0);
digitalWrite(IN1_PIN, LOW);
digitalWrite(IN2_PIN, LOW);
// SET THE VARIABLES
millisPower = millis();
// BLINK THE LED
for (int x = 0; x < 3; x++)
{
digitalWrite(ledA, HIGH);
if (millis() > (millisPower + 2500)) {} // This is not going to delay at all
digitalWrite(ledA, LOW);
if (millis() > (millisPower + 2500)) {} // This is not going to delay at all
}
}
/////////////////////////// LOOP //////////////////////////////////
void loop ()
{
if (Serial.available() > 0)
{
// Checks whether data is comming from the serial port
inputString = Serial.readString(); // Reads the data from the serial port
}
// STOPPED
if (inputString.startsWith("STP"))
{
PRINT_D(PLAYER);
analogWrite(ENA_PIN, 0);
counter = 0;
}
// DIRECTION
if (inputString.startsWith("FWD"))
{
PRINT_C(FORWARD);
digitalWrite(IN1_PIN, LOW);
digitalWrite(IN2_PIN, HIGH);
}
// DIRECTION
if (inputString.startsWith("REV"))
{
PRINT_C(REVERSE);
digitalWrite(IN1_PIN, HIGH);
digitalWrite(IN2_PIN, LOW);
}
MYWAIT(10); // This is not going to delay at all
// SPEED
if (inputString[0] == '*')
{
PRINT_E(0) ;
inputString.remove(0, 1); // Remove the '*'
counter = inputString.toInt();
speedTrain = speedArray[counter];
analogWrite(ENA_PIN, speedTrain);
}
}
//////////////////////////// FUNCTIONS /////////////////////////////
Thank you for the clarification, I was trying to get a handle on how/did macros work - using the Delay(?) as a sample.
Perhaps I should have explained myself a little better on my original post.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.