Macro and variable error in code

Hi all

New to arduino and in process of building my first proper sketch for a project . I've been verifying each section as I go, but i've run into an error and I cant work out what the root cause is...another section of code using the same logic verified ok...

Any pointers would be greatly appreciated!

Thanks

Error is below, followed by the code:

Arduino: 1.8.19 (Windows 10), Board: "Arduino Uno"

In file included from sketch\EBIKE_CONTROL_SKETCH_V1.ino.cpp:1:0:

C:\Users\Tim\Documents\Arduino\EBIKE_CONTROL_SKETCH_V1\EBIKE_CONTROL_SKETCH_V1.ino: In function 'void loop()':

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:40:14: error: assignment of read-only variable 'ModeSwitch3'

#define HIGH 0x1

          ^

C:\Users\Tim\Documents\Arduino\EBIKE_CONTROL_SKETCH_V1\EBIKE_CONTROL_SKETCH_V1.ino:54:26: note: in expansion of macro 'HIGH'

else if (ModeSwitch3 = HIGH) {ModeState=Mode3;

                      ^~~~

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:41:14: error: lvalue required as left operand of assignment

#define LOW 0x0

          ^

C:\Users\Tim\Documents\Arduino\EBIKE_CONTROL_SKETCH_V1\EBIKE_CONTROL_SKETCH_V1.ino:56:47: note: in expansion of macro 'LOW'

else if (ModeSwitch1 = LOW && ModeSwitch3 = LOW) {ModeState=Mode2;

                                           ^~~

exit status 1

#include <Servo.h>

Servo ESC;     // create servo object to control the ESC

//Hardware constants
const int TorqueInput = A0; // input from torque sensor (0-5v)
const int PASInput = 2; // input from cadence sensor (HIGH/LOW)
const int ModeSwitch1 = 3; // input from mode switch setting 1 (switching green and black)(HIGH/LOW)
const int ModeSwitch3 = 4; // input from mode switch setting 3 (switching red and black)(HIGH/LOW)
const int CurrentSensor = A1; // input from current sensor (0-5v)
const int OnOffSwitch = 5; // input from on/off switch (HIGH/LOW)

//Software constants
const unsigned long StartPulsesTimeout = 150; // Time within which pulses set by startpulses must be counted to identify pedalling
const int StartPulses = 3; // Number of PAS pulses needed before turning on
const float Mode1 = 0.33; // modifier for Mode 1
const float Mode2 = 0.66; // modifier for Mode 2
const float Mode3 = 1.00; // modifier for Mode 3

// Variables
int OnOffState = 0; // variable holding information on state of OnOffSwitch (1 or 0)
int PASState = 0; // variable holding information on whether pedalling is occurring (1 or 0)
volatile int InputEdges = 0; // counter for the number of pulses since last reset
volatile unsigned long LastEdgeTime = 0; // timestamp of last PAS pulse
bool state=false; // variable holding information about the state of the output
volatile int PulseCounter; // variable holding pulse count
int ModeState = 0; // variable holding state of Mode Switch


void setup() {
  // Attach the ESC on pin 1
  ESC.attach(1,1000,2000); // (pin, min pulse width, max pulse width in microseconds) 
  Serial.begin(9600); // initialize serial communication at 9600 bits per second:
  pinMode(OnOffSwitch, INPUT); // make the OnOffSwitch pin an input:
  pinMode(PASInput, INPUT); // make the PASInput pin an input
  pinMode(ModeSwitch1, INPUT); // make the ModeSwitch1 pin an input
  pinMode(ModeSwitch3, INPUT); // make the ModeSwitch3 pin an input

}

void loop() {
  // Check status of OnOffSwitch
  int OnOffSwitchState = digitalRead(OnOffSwitch); // read the input pin:
  if (OnOffSwitchState == HIGH){
    OnOffState=1;
    }
  else {OnOffState=0;
  }

  // Check status of the Mode Switch
  if (ModeSwitch1 == HIGH) {
    ModeState=Mode1;
  }
  else if (ModeSwitch3 = HIGH) {ModeState=Mode3;
  }
  else if (ModeSwitch1 = LOW && ModeSwitch3 = LOW) {ModeState=Mode2;
  }

   // If PAS signal is inactive for too long, turn off everything
  unsigned long CurrentTime=millis();
  if ((CurrentTime>LastEdgeTime)&&((CurrentTime-LastEdgeTime)>StartPulsesTimeout)) {
    TurnOff();
  }
  
  // If PulseCounter equal or greater than StartPulses, make PASState = 1
  if (PulseCounter>=StartPulses) {PASState=1;}
}


void pulse () {
    // check status of PASInput
    // Interrupt subroutine, refresh last impulse timestamp and increment pulse counter 
  attachInterrupt(digitalPinToInterrupt(PASInput), []() { PulseCounter++;  }, RISING);
}


void TurnOff() {
  //Turn off output, reset pulse counter and set state variable to false
  noInterrupts();
  InputEdges=0;
  state=false;
  OnOffState=0;
  interrupts();
}
  else if (ModeSwitch3 = HIGH) {ModeState=Mode3;

= for assignment
==for comparison

Which did you mean to use ?

HIGH is already defined, print it to see what you get.


else if (ModeSwitch3 = HIGH)
This is not correct, research = vs ==


each { needs a friend }

Thankyou!

I don't know how I missed that. I thought it was something to do with having no variable assigned. Of course it verifies fine now.

I stared at that for half an hr last night...

I think that this piece of code does nothing because the ModeSwitch1 variable never is updated.

  if (ModeSwitch1 == HIGH) 

For me it should be:

if (digitalRead(ModeSwitch1) == HIGH)

Try this code.
I think it should do what do you want.

#include <Servo.h>

Servo ESC;     // create servo object to control the ESC

// Hardware constants
const int TorqueInput = A0;   // input from torque sensor (0-5v)
const int PASInput = 2;       // input from cadence sensor (HIGH/LOW)
const int ModeSwitch1 = 3;    // input from mode switch setting 1 (switching green and black)(HIGH/LOW)
const int ModeSwitch3 = 4;    // input from mode switch setting 3 (switching red and black)(HIGH/LOW)
const int CurrentSensor = A1; // input from current sensor (0-5v)
const int OnOffSwitch = 5;    // input from on/off switch (HIGH/LOW)

// Software constants
const unsigned long StartPulsesTimeout = 150; // Time within which pulses set by startpulses must be counted to identify pedalling
const int StartPulses = 3;                    // Number of PAS pulses needed before turning on
const float Mode1 = 0.33;                     // modifier for Mode 1
const float Mode2 = 0.66;                     // modifier for Mode 2
const float Mode3 = 1.00;                     // modifier for Mode 3

// Variables
int OnOffState = 0;                      // variable holding information on state of OnOffSwitch (1 or 0)
int PASState = 0;                        // variable holding information on whether pedalling is occurring (1 or 0)
volatile int InputEdges = 0;             // counter for the number of pulses since last reset
volatile unsigned long LastEdgeTime = 0; // timestamp of last PAS pulse
bool state = false;                      // variable holding information about the state of the output
volatile int PulseCounter;               // variable holding pulse count
int ModeState = 0;                       // variable holding state of Mode Switch


void pulse ()
{
  // check status of PASInput
  // Interrupt subroutine, refresh last impulse timestamp and increment pulse counter
   attachInterrupt(digitalPinToInterrupt(PASInput), []() { PulseCounter++;  }, RISING);
}


void TurnOff()
{
  // Turn off output, reset pulse counter and set state variable to false
  noInterrupts();
  InputEdges = 0;
  state = false;
  OnOffState = 0;
  interrupts();
}

void setup()
{
  // Attach the ESC on pin 1
  ESC.attach(1,1000,2000);     // (pin, min pulse width, max pulse width in microseconds)
  Serial.begin(9600);          // initialize serial communication at 9600 bits per second:
  pinMode(OnOffSwitch, INPUT); // make the OnOffSwitch pin an input:
  pinMode(PASInput, INPUT);    // make the PASInput pin an input
  pinMode(ModeSwitch1, INPUT); // make the ModeSwitch1 pin an input
  pinMode(ModeSwitch3, INPUT); // make the ModeSwitch3 pin an input

}

void loop()
{
  // Check status of OnOffSwitch
  if (digitalRead(OnOffSwitch) == HIGH)
  {
    OnOffState = 1;
  }
  else
  {
    OnOffState = 0;
  }

  // Check status of the Mode Switch
  if (digitalRead(ModeSwitch1) == HIGH)
  {
    ModeState = Mode1;
  }
  else if (digitalRead(ModeSwitch3) == HIGH)
  {
    ModeState = Mode3;
  }
  else if ((digitalRead(ModeSwitch1) == LOW) && digitalRead(ModeSwitch3 == LOW))
  {
    ModeState = Mode2;
  }

  // If PAS signal is inactive for too long, turn off everything
  unsigned long CurrentTime = millis();
  if ((CurrentTime > LastEdgeTime) && ((CurrentTime - LastEdgeTime) > StartPulsesTimeout))
  {
    TurnOff();
  }

  // If PulseCounter equal or greater than StartPulses, make PASState = 1
  if (PulseCounter >= StartPulses)
  {
    PASState = 1;
  }
}

Thanks - that was a good spot!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.