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
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
#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