Why doesnt the other code work?

I have been writing a code that controls 2 motors for a shredder. There is a switch on the lid and i need to turn the first motor on after it has went high, low high.(when you open to put something in and close it again).
The second motor is for a big "screw" that moves the shredded material.
A photoelectric switch shuts off the second motor if it does not detect anything falling by it for 10 seconds. The motor needs to stop when the lid goes low while it runs(for safety reasons). Here is where the problem lies.

i have 2 codes:
One that works but does not turn the motor off when the lid is opened.
One that does not work :frowning:

Here is the code that works:

#include <SPI.h>
#include <Controllino.h>

// Define the pins used by the components
const int startButtonPin = CONTROLLINO_A2;
const int stopButtonPin = CONTROLLINO_A3;
const int lidSwitchPin = CONTROLLINO_A0;
const int motor1Pin = CONTROLLINO_D4;
const int motor2Pin = CONTROLLINO_D5;
const int photoSensorPin = CONTROLLINO_A1;

// Define the duration for the motors to run
const int motor1Duration = 20000; // 20 seconds
const int motor2wait = 10000;
const int motor2Duration = 30000; // 30 seconds
const int idleDuration = 10000; // 10 seconds

int lid1 = 0;
int lid2 = 0;
int started = 0;
int photoswitch = 0;

// Define the state of the program
bool running = false;
bool motor1Running = false;
bool motor2Running = false;
bool photodetect = false;

// Define the time when each component was last turned on
unsigned long motor1StartTime = 0;
unsigned long motor2StartTime = 0;
unsigned long lastPhotoDetectionTime = 0;

void setup() {
  // Set the pins used by the components as inputs or outputs
  pinMode(startButtonPin, INPUT_PULLUP);
  pinMode(stopButtonPin, INPUT_PULLUP);
  pinMode(lidSwitchPin, INPUT_PULLUP);
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(photoSensorPin, INPUT);

  // Turn off the motors initially
  digitalWrite(motor1Pin, LOW);
  digitalWrite(motor2Pin, LOW);


}

void loop() {
  // Check if the start button was pressed
  if (digitalRead(startButtonPin) == HIGH) {
    started = HIGH;
  }
if (started == HIGH && digitalRead(lidSwitchPin) == HIGH){
  lid1 = HIGH;
}
if (lid1 == HIGH && digitalRead(lidSwitchPin) == LOW){
 lid2 = HIGH;
 lid1 = LOW;
 delay(500);
 }
if (lid2 == HIGH && digitalRead(lidSwitchPin) == HIGH){
    lid2 = LOW;
    motor1Running = true;
    motor1StartTime = millis();
    digitalWrite(motor1Pin, HIGH); 
}

  if (motor1Running && (millis() - motor1StartTime >= motor2wait)){
      motor2Running = true;
    motor2StartTime = millis();
    digitalWrite(motor2Pin, HIGH);
  }
  
  // Check if motor1 has run for the required duration
  if (motor1Running && (millis() - motor1StartTime >= motor1Duration)) {
    motor1Running = false;
    digitalWrite(motor1Pin, LOW);
  }


  // Check if motor2 has run for the required duration
  if (motor2Running && (millis() - motor2StartTime >= motor2Duration)) {
    // Check the photosensor
  photoswitch = HIGH;
    }
  
  if(photoswitch == HIGH){
    photodetect = true;

  }  

  if(photoswitch == HIGH && photoSensorPin == LOW){
    lastPhotoDetectionTime = millis();
  }

if(photodetect && millis() - lastPhotoDetectionTime >= idleDuration){
  digitalWrite(motor2Pin, LOW);
  motor2Running = false;
  photodetect = false;
  photoswitch = LOW;  
  }

  // Check if the stop button or lid switch was pressed
  if (digitalRead(stopButtonPin) == HIGH) {
    running = false;
    motor1Running = false;
    motor2Running = false;
    digitalWrite(motor1Pin, LOW);
    digitalWrite(motor2Pin, LOW);
    started = LOW;
  }
}

This code does not work :frowning:

#include <SPI.h>
#include <Controllino.h>

// Define the pins used by the components
const int startButtonPin = CONTROLLINO_A2;
const int stopButtonPin = CONTROLLINO_A3;
const int lidSwitchPin = CONTROLLINO_A0;
const int motor1Pin = CONTROLLINO_D4;
const int motor2Pin = CONTROLLINO_D5;
const int photoSensorPin = CONTROLLINO_A1;

// Define the duration for the motors to run
const int motor1Duration = 20000; // 20 seconds
const int motor2wait = 10000;
const int motor2Duration = 30000; // 30 seconds
const int idleDuration = 10000; // 10 seconds

int lid1 = 0;
int lid2 = 0;
int started = 0;
int photoswitch = 0;

// Define the state of the program
bool running = false;
bool motor1Running = false;
bool motor2Running = false;
bool photodetect = false;

// Define the time when each component was last turned on
unsigned long motor1StartTime = 0;
unsigned long motor2StartTime = 0;
unsigned long lastPhotoDetectionTime = 0;

void setup() {
  // Set the pins used by the components as inputs or outputs
  pinMode(startButtonPin, INPUT_PULLUP);
  pinMode(stopButtonPin, INPUT_PULLUP);
  pinMode(lidSwitchPin, INPUT_PULLUP);
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(photoSensorPin, INPUT);

  // Turn off the motors initially
  digitalWrite(motor1Pin, LOW);
  digitalWrite(motor2Pin, LOW);


}

void loop() {
  // Check if the start button was pressed
  if (digitalRead(startButtonPin) == HIGH) {
    started = HIGH;
  }
if (started == HIGH && digitalRead(lidSwitchPin) == HIGH){
  lid1 = HIGH;
}
if (lid1 == HIGH && digitalRead(lidSwitchPin) == LOW){
 lid2 = HIGH;
 lid1 = LOW;
 delay(500);
 }
if (lid2 == HIGH && digitalRead(lidSwitchPin) == HIGH){
    lid2 = LOW;
    motor1Running = true;
    motor1StartTime = millis();
    digitalWrite(motor1Pin, HIGH); 
}

  if (motor1Running && (millis() - motor1StartTime >= motor2wait)){
      motor2Running = true;
    motor2StartTime = millis();
    digitalWrite(motor2Pin, HIGH);
  }
  
  // Check if motor1 has run for the required duration
  if (motor1Running && (millis() - motor1StartTime >= motor1Duration)) {
    motor1Running = false;
    digitalWrite(motor1Pin, LOW);
  }


  // Check if motor2 has run for the required duration
  if (motor2Running && (millis() - motor2StartTime >= motor2Duration)) {
    // Check the photosensor
  photoswitch = HIGH;
    }
  
  if(photoswitch == HIGH){
    photodetect = true;

  }  

  if(photoswitch == HIGH && photoSensorPin == LOW){
    lastPhotoDetectionTime = millis();
  }

if(photodetect && millis() - lastPhotoDetectionTime >= idleDuration){
  digitalWrite(motor2Pin, LOW);
  motor2Running = false;
  photodetect = false;
  photoswitch = LOW;  
  }

if(motor1Running && digitalRead(lidSwitchPin == LOW)){
 running = false;
    motor1Running = false;
    motor2Running = false;
    digitalWrite(motor1Pin, LOW);
    digitalWrite(motor2Pin, LOW);
    photodetect = false;
    photoswitch = LOW;  
    started = LOW; 
} 

if(motor2Running && digitalRead(lidSwitchPin == LOW)){
 running = false;
    motor1Running = false;
    motor2Running = false;
    digitalWrite(motor1Pin, LOW);
    digitalWrite(motor2Pin, LOW);
    photodetect = false;
    photoswitch = LOW;  
    started = LOW; 
}
 
  if (digitalRead(stopButtonPin) == HIGH) {
    running = false;
    motor1Running = false;
    motor2Running = false;
    digitalWrite(motor1Pin, LOW);
    digitalWrite(motor2Pin, LOW);
    photodetect = false;
    photoswitch = LOW;  
    started = LOW;
}

If you have looked it through and have discovered any reasons and maybe some solutions you are a champ :smiley:

As so often poetry descriptions confuse more then they explain. A picture of the arrangement and a flow chart description of the logic of the code.

An annotated schematic and links to the hardware items technical information would also help.

The syntax for the digitalRead of the lidSwitchPin is incorrect.

//if(motor1Running && digitalRead(lidSwitchPin == LOW)){
if(motor1Running && digitalRead(lidSwitchPin) == LOW)){
 running = false;
    motor1Running = false;
    motor2Running = false;
    digitalWrite(motor1Pin, LOW);
    digitalWrite(motor2Pin, LOW);
    photodetect = false;
    photoswitch = LOW;  
    started = LOW; 
} 

//if(motor2Running && digitalRead(lidSwitchPin == LOW)){
if(motor2Running && digitalRead(lidSwitchPin) == LOW)){
 running = false;
    motor1Running = false;
    motor2Running = false;
    digitalWrite(motor1Pin, LOW);
    digitalWrite(motor2Pin, LOW);
    photodetect = false;
    photoswitch = LOW;  
    started = LOW; 
}

Your code is a little confusing to me in that with INPUT_PULLUP for the pin mode, some of the expected HIGH/LOW states seem wrong.

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