Microswitch permanently left on

I have had a problem where the microswitches despite me pressing them have reregistered as on. I have used some other code and they work. It seems to be a code issue rather than physical components/ wiring.

I have attached the effective code here:

// Library
  #include <Unit_Sonic.h>
  #include <Wire.h>
  #include <Adafruit_TCS34725.h>
// Pin definitions 
  #define Mircoswitch1_PIN 2
  #define LASER_PIN 3 
  #define SENSOR_PIN 14   //A0
  #define Mircoswitch2_PIN 15 //A1
  #define M5switchBLUE_PIN 16 //A2 
  #define M5switchRED_PIN 17 //A3 

  // JGA_RIGHT
    int enA = 10;
    int in1_JGA = 9; 
    int in2_JGA = 8;  
  //JGA_LEFT
    int enB = 11;  
    int in3_JGA = 12; 
    int in4_JGA = 13;
  //TT_RIGHT
    int in1_TT = 4;
    int in2_TT = 5; 
  //TT_LEFT
    int in3_TT = 6;  
    int in4_TT = 7;  

//States 
  int noBeamCount = 0;  // Variable to count times no laser beam is detected
  int Mircoswitch1 = HIGH;
  int Mircoswitch2 = HIGH;
  int M5switchBLUE = HIGH;
  int M5switchRED = HIGH;
  bool blueMode = false;
  bool redMode = false;
// Initializations
  // Color sensor initialization
    Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_614MS, TCS34725_GAIN_1X);
  //Ultrasonic initialization
    SONIC_I2C ultrasonic;
// Voids
  void getRawData(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c);
  void stopAllMotors();
  void moveJGAForward(int speed);
  void moveJGALeft(int speed);
  void moveJGARight(int speed);
  void moveTTForward();
  void sensors();
void setup() {
 Serial.begin(9600);
 //Configure pins 
  // Switch Pullup
    pinMode(Mircoswitch1, INPUT_PULLUP); // Micro switch
    pinMode(Mircoswitch2, INPUT_PULLUP); // Micro switch
    pinMode(M5switchBLUE, INPUT_PULLUP); // M5 Stack switch
    pinMode(M5switchRED, INPUT_PULLUP); // M5 Stack switch
  //Motors 
    //JGA_Motors
      pinMode(enA, OUTPUT);
      pinMode(enB, OUTPUT);
      pinMode(in1_JGA, OUTPUT);
      pinMode(in2_JGA, OUTPUT);
      pinMode(in3_JGA, OUTPUT);
      pinMode(in4_JGA, OUTPUT); 
    //TT_Motors
      pinMode(in1_TT, OUTPUT);
      pinMode(in2_TT, OUTPUT);
      pinMode(in3_TT, OUTPUT);
      pinMode(in4_TT, OUTPUT);
  //Laser
    pinMode(LASER_PIN, OUTPUT);
    digitalWrite(LASER_PIN, LOW);
 //Start sensors 
  Wire.begin();
  ultrasonic.begin(&Wire, 0x57, A4, A5, 100000L);

  if (tcs.begin()) {
    Serial.println("Color sensor found.");
    tcs.write8(TCS34725_PERS, TCS34725_PERS_NONE);  // Generate interrupt for every RGB cycle
  } else {
    Serial.println("Color sensor not detected.");
 }
  tcs.write8(TCS34725_PERS, TCS34725_PERS_NONE);
 
 
  Serial.flush();
 //MOTORS_OFF
  stopAllMotors();
}
void loop() {

 //Check switches
  Mircoswitch1 = digitalRead(2);
  Mircoswitch2 = digitalRead(15);
  M5switchBLUE = digitalRead(16);
  M5switchRED = digitalRead(17);
 //M5stack switches
  //Blue 
    if (M5switchBLUE == LOW) {
    Serial.println("M5 Stack Switch blue Pressed");
    blueMode = true;
    redMode = false;
    }
  //Red   
    if (M5switchRED == LOW) {
    Serial.println("M5 Stack Switch red Pressed");
    blueMode = false;
    redMode = true;
    }
  //Blue mode 
    if (blueMode) {
    uint16_t r, g, b, c, colorTemp, lux;
    getRawData_noDelay(&r, &g, &b, &c);
    colorTemp = tcs.calculateColorTemperature(r, g, b);
    lux = tcs.calculateLux(r, g, b);
   
    Serial.print("Color Temp: "); Serial.print(colorTemp, DEC); Serial.print(" K - ");
    Serial.print("Lux: "); Serial.print(lux, DEC); Serial.print(" - ");
    Serial.print("R: "); Serial.print(r, DEC); Serial.print(" ");
    Serial.print("G: "); Serial.print(g, DEC); Serial.print(" ");
    Serial.print("B: "); Serial.print(b, DEC); Serial.print(" ");
    Serial.print("C: "); Serial.print(c, DEC); Serial.print(" ");
    Serial.println(" ");
    Serial.flush();
   
 
    if (r >150 && r < 250 && g > 250 && g < 350 && b > 125 && b < 175 ){
      Serial.println("COURT");
      stopAllMotors();
      delay(1000);
      moveJGARight(200);
  }
    Serial.println("M5 Stack Switch Blue Mode");
    moveJGAForward(200);
    sensors();
    }
  //Red mode
     if (redMode) {
    Serial.println("M5 Stack Switch Red Mode");
    moveJGAForward(200);
    sensors();
    }
}

//Voids 
void getRawData_noDelay(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c) {
  *c = tcs.read16(TCS34725_CDATAL);
  *r = tcs.read16(TCS34725_RDATAL);
  *g = tcs.read16(TCS34725_GDATAL);
  *b = tcs.read16(TCS34725_BDATAL);
}
void sensors() {
  //Mircoswitches 
    //Left switch
      if (Mircoswitch1== LOW) {
      Serial.println("Micro Switch 1 Pressed");
      moveJGARight(200);}
    //Right switch
      if (Mircoswitch2 == LOW) {
      Serial.println("Micro Switch 2 Pressed");
      moveJGALeft(200);}

  //Laser beam
  digitalWrite(LASER_PIN, HIGH);
  delay(1000);  
  int sensorValue = analogRead(SENSOR_PIN);
  Serial.print("Sensor Value: ");
  Serial.println(sensorValue);
  if (sensorValue > 100) {  
    Serial.println("Laser beam detected!");
  } else {
    Serial.println("No laser beam detected.");
  }
}
//Motor 
void moveJGAForward(int speed) {
  analogWrite(enA, speed); // Set speed for Right motor
  analogWrite(enB, speed); // Set speed for Left motor
  
  digitalWrite(in1_JGA, HIGH);
  digitalWrite(in2_JGA, LOW); 
  digitalWrite(in3_JGA, LOW);
  digitalWrite(in4_JGA, HIGH); 
}
void moveJGALeft(int speed){
    analogWrite(enA, speed); // Set speed for Right motor
    analogWrite(enB, speed); // Set speed for Left motor

    digitalWrite(in1_JGA, HIGH);
    digitalWrite(in2_JGA, LOW); 
    digitalWrite(in3_JGA, HIGH);
    digitalWrite(in4_JGA, LOW); 
}
void moveJGARight(int speed){
  analogWrite(enA, speed); // Set speed for Right motor
  analogWrite(enB, speed); // Set speed for Left motor
  
  digitalWrite(in1_JGA, LOW);
  digitalWrite(in2_JGA, HIGH); 
  digitalWrite(in3_JGA, LOW);
  digitalWrite(in4_JGA, HIGH); 
}
void moveTTForward() {
  digitalWrite(in1_TT, LOW);
  digitalWrite(in2_TT, HIGH); // Right motor forward
  digitalWrite(in3_TT, LOW);
  digitalWrite(in4_TT, HIGH); // Left motor forward
}
void stopAllMotors() {
  // JGA_Motors
  digitalWrite(in1_JGA, LOW);
  digitalWrite(in2_JGA, LOW);
  digitalWrite(in3_JGA, LOW);
  digitalWrite(in4_JGA, LOW);
  analogWrite(enA, 0);
  analogWrite(enB, 0);
  //TT_Motors
  digitalWrite(in1_TT, LOW);
  digitalWrite(in2_TT, LOW);
  digitalWrite(in3_TT, LOW);
  digitalWrite(in4_TT, LOW);
}

Anyone got any ideas?

What leads you to think they are stuck on?

It would be simple to debug I think, just add some prints.

    pinMode(Mircoswitch1, INPUT_PULLUP);  // Micro switch
    pinMode(Mircoswitch2, INPUT_PULLUP);  // Micro switch

Which pin numbers are you setting the pinMode() for in these 2 statements ?

Here is a clue

int Mircoswitch1 = HIGH;
int Mircoswitch2 = HIGH;
1 Like

I have got it so it prints when the switches are pressed and they always come up when I look at the serial monitor so it suggest that is may be pressed on.

You didn't show a schematic and you've got an awful lot of code before you tested the switches! Everything should be tested separately before you put everything together... It's a LOT easier to test/troubleshoot/verify that way. The Digital Read Serial example is an easy way to test switches.

You didn't show us your wiring but since you are using INPUT-PULLUP the switch should be wired to ground. The input should read high until the switch is activated "overpowering" the pull-up and forcing the input low.

Also, most microswitches are SPDT with 3 terminals so it could be wired wrong.

I would be interested in hearing the answer to my question in post #3

1 Like

On my excel spreadsheet they are on print A1(D15) and D2, unfortunately they are split over analogue and digital which makes it a bit more difficult.

I have a had a go at changing the code but I doesn't seemed to have worked.

The switches are wired on the ground and NO terminal. Also the switches performed fine when I have run code which I know works as I have done it in stage where until I have started to put the different sketches together this is where I have run into issues.
The only difference physical is the movement to a different pin.

Here's another clue to go with @UKHeliBob's:

Looking through the IDE installation we find

grep HIGH *
Arduino.h:#define HIGH 0x1

a7

What value will the Mircoswitch1 variable have after you do

int Mircoswitch1 = HIGH;

Thanks for the help, I can see where the problem with the variable being set a just high was, I have now edited it and it works successfully.

Please post your edited, working sketch for the benefit of future readers of this topic

Here is the edited code which works.

// Library
  #include <Unit_Sonic.h>
  #include <Wire.h>
  #include <Adafruit_TCS34725.h>
// Pin definitions 
  #define Mircoswitch1_PIN 2
  #define LASER_PIN 3 
  #define SENSOR_PIN 14   //A0
  #define Mircoswitch2_PIN 15 //A1
  #define M5switchBLUE_PIN 16 //A2 
  #define M5switchRED_PIN 17 //A3 

  // JGA_RIGHT
    int enA = 10;
    int in1_JGA = 9; 
    int in2_JGA = 8;  
  //JGA_LEFT
    int enB = 11;  
    int in3_JGA = 12; 
    int in4_JGA = 13;
  //TT_RIGHT
    int in1_TT = 4;
    int in2_TT = 5; 
  //TT_LEFT
    int in3_TT = 6;  
    int in4_TT = 7;  

//States 
  int noBeamCount = 0;  // Variable to count times no laser beam is detected
  int Mircoswitch2 = HIGH;
  int Mircoswitch15 = HIGH;
  int M5switchBLUE = HIGH;
  int M5switchRED = HIGH;
  bool blueMode = false;
  bool redMode = false;
// Initializations
  // Color sensor initialization
    Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_614MS, TCS34725_GAIN_1X);
  //Ultrasonic initialization
    SONIC_I2C ultrasonic;
// Voids
  void getRawData(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c);
  void stopAllMotors();
  void moveJGAForward(int speed);
  void moveJGALeft(int speed);
  void moveJGARight(int speed);
  void moveTTForward();
  void sensors();
void setup() {
 Serial.begin(9600);
 //Configure pins 
  // Switch Pullup
    pinMode(Mircoswitch2, INPUT_PULLUP); // Micro switch
    pinMode(Mircoswitch15, INPUT_PULLUP); // Micro switch
    pinMode(M5switchBLUE, INPUT_PULLUP); // M5 Stack switch
    pinMode(M5switchRED, INPUT_PULLUP); // M5 Stack switch
  //Motors 
    //JGA_Motors
      pinMode(enA, OUTPUT);
      pinMode(enB, OUTPUT);
      pinMode(in1_JGA, OUTPUT);
      pinMode(in2_JGA, OUTPUT);
      pinMode(in3_JGA, OUTPUT);
      pinMode(in4_JGA, OUTPUT); 
    //TT_Motors
      pinMode(in1_TT, OUTPUT);
      pinMode(in2_TT, OUTPUT);
      pinMode(in3_TT, OUTPUT);
      pinMode(in4_TT, OUTPUT);
  //Laser
    pinMode(LASER_PIN, OUTPUT);
    digitalWrite(LASER_PIN, LOW);
 //Start sensors 
  Wire.begin();
  ultrasonic.begin(&Wire, 0x57, A4, A5, 100000L);

  if (tcs.begin()) {
    Serial.println("Color sensor found.");
    tcs.write8(TCS34725_PERS, TCS34725_PERS_NONE);  // Generate interrupt for every RGB cycle
  } else {
    Serial.println("Color sensor not detected.");
 }
  tcs.write8(TCS34725_PERS, TCS34725_PERS_NONE);
 
 
  Serial.flush();
 //MOTORS_OFF
  stopAllMotors();
}
void loop() {

 //Check switches
  Mircoswitch2 = digitalRead(2);
  Mircoswitch15 = digitalRead(15);
  M5switchBLUE = digitalRead(16);
  M5switchRED = digitalRead(17);
 //M5stack switches
  //Blue 
    if (M5switchBLUE == LOW) {
    Serial.println("M5 Stack Switch blue Pressed");
    blueMode = true;
    redMode = false;
    }
  //Red   
    if (M5switchRED == LOW) {
    Serial.println("M5 Stack Switch red Pressed");
    blueMode = false;
    redMode = true;
    }
  //Blue mode 
    if (blueMode) {
    uint16_t r, g, b, c, colorTemp, lux;
    getRawData_noDelay(&r, &g, &b, &c);
    colorTemp = tcs.calculateColorTemperature(r, g, b);
    lux = tcs.calculateLux(r, g, b);
   
    Serial.print("Color Temp: "); Serial.print(colorTemp, DEC); Serial.print(" K - ");
    Serial.print("Lux: "); Serial.print(lux, DEC); Serial.print(" - ");
    Serial.print("R: "); Serial.print(r, DEC); Serial.print(" ");
    Serial.print("G: "); Serial.print(g, DEC); Serial.print(" ");
    Serial.print("B: "); Serial.print(b, DEC); Serial.print(" ");
    Serial.print("C: "); Serial.print(c, DEC); Serial.print(" ");
    Serial.println(" ");
    Serial.flush();
   
 
    if (r >150 && r < 250 && g > 250 && g < 350 && b > 125 && b < 175 ){
      Serial.println("COURT");
      stopAllMotors();
      delay(1000);
      moveJGARight(200);
  }
    Serial.println("M5 Stack Switch Blue Mode");
    moveJGAForward(200);
    sensors();
    }
  //Red mode
     if (redMode) {
    Serial.println("M5 Stack Switch Red Mode");
    moveJGAForward(200);
    sensors();
    }
}

//Voids 
void getRawData_noDelay(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c) {
  *c = tcs.read16(TCS34725_CDATAL);
  *r = tcs.read16(TCS34725_RDATAL);
  *g = tcs.read16(TCS34725_GDATAL);
  *b = tcs.read16(TCS34725_BDATAL);
}
void sensors() {
  //Mircoswitches 
    //Left switch
      if (Mircoswitch2== LOW) {
      Serial.println("Micro Switch 1 Pressed");
      moveJGARight(200);}
    //Right switch
      if (Mircoswitch15 == LOW) {
      Serial.println("Micro Switch 2 Pressed");
      moveJGALeft(200);}

  //Laser beam
  digitalWrite(LASER_PIN, HIGH);
  delay(1000);  
  int sensorValue = analogRead(SENSOR_PIN);
  Serial.print("Sensor Value: ");
  Serial.println(sensorValue);
  if (sensorValue > 100) {  
    Serial.println("Laser beam detected!");
  } else {
    Serial.println("No laser beam detected.");
  }
}
//Motor 
void moveJGAForward(int speed) {
  analogWrite(enA, speed); // Set speed for Right motor
  analogWrite(enB, speed); // Set speed for Left motor
  
  digitalWrite(in1_JGA, HIGH);
  digitalWrite(in2_JGA, LOW); 
  digitalWrite(in3_JGA, LOW);
  digitalWrite(in4_JGA, HIGH); 
}
void moveJGALeft(int speed){
    analogWrite(enA, speed); // Set speed for Right motor
    analogWrite(enB, speed); // Set speed for Left motor

    digitalWrite(in1_JGA, HIGH);
    digitalWrite(in2_JGA, LOW); 
    digitalWrite(in3_JGA, HIGH);
    digitalWrite(in4_JGA, LOW); 
}
void moveJGARight(int speed){
  analogWrite(enA, speed); // Set speed for Right motor
  analogWrite(enB, speed); // Set speed for Left motor
  
  digitalWrite(in1_JGA, LOW);
  digitalWrite(in2_JGA, HIGH); 
  digitalWrite(in3_JGA, LOW);
  digitalWrite(in4_JGA, HIGH); 
}
void moveTTForward() {
  digitalWrite(in1_TT, LOW);
  digitalWrite(in2_TT, HIGH); // Right motor forward
  digitalWrite(in3_TT, LOW);
  digitalWrite(in4_TT, HIGH); // Left motor forward
}
void stopAllMotors() {
  // JGA_Motors
  digitalWrite(in1_JGA, LOW);
  digitalWrite(in2_JGA, LOW);
  digitalWrite(in3_JGA, LOW);
  digitalWrite(in4_JGA, LOW);
  analogWrite(enA, 0);
  analogWrite(enB, 0);
  //TT_Motors
  digitalWrite(in1_TT, LOW);
  digitalWrite(in2_TT, LOW);
  digitalWrite(in3_TT, LOW);
  digitalWrite(in4_TT, LOW);
}

Maybe, but if it is working then it is more by luck than judgement

Consider the following snippets copied from your sketch

int Mircoswitch2 = HIGH;

What value will the Mircoswitch2 variable have after this statement is executed ?

    pinMode(Mircoswitch2, INPUT_PULLUP);   // Micro switch

Which pin number are you setting to INPUT_PULLUP with this statement ?

The hole point of doing this

# define M5switchBLUE_PIN 16

is that you can later say things like

  M5switchBLUE = digitalRead(16);

without using a hard-coded number, viz:

  M5switchBLUE = digitalRead(M5switchBLUE_PIN);

It is more informative and makes it easier for you, or anyone, to modify.

BTW it seems certain that you have microswitches, not mircoswitches. Fortunately that will not bother the final arbiter in the least as long as you are consistent.

a7

https://docs.arduino.cc/language-reference/en/functions/digital-io/pinMode/

@beng07 I would still be interested in seeing your answers to my questions as you have made a serious mistake in your code even if it works as you want.

If you do not learn from this mistake then you may repeat in future projects so it is best avoided

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