Motor Control Pushbutton Control w/ Switch between two seperate sensor codes

Hello!

The goal here is to control a motor with 3 different pushbuttons (forward, reverse, and stop) while also including a switch between two loops for motor stop if sensors read that a bin is now "full" (ultrasonic and a photocell light curtain). I am still learning and would appreciate any help with this project, if its possible.

Attempt3.ino (4.22 KB)

FwdStopRvrs.ino (1.13 KB)

Yeah first time post, realized pretty quick it was done wrong...sorry about that!

This project is for a motor spinning a reciprocating screw that feeds into a bin. The buttons are meant for motor directional control where forward is feeding into the bin. Additionally, the sensors will detect if the bin is full and tell the motor to stop. Right now, it's two separate codes and I need help combining them into a single code with a method of switching between the "UltrasonicCode" loop and "PhotocellCode" loop.

Code for Forward, Reverse, and Stop

#define MOTORFORWARD 13
#define MOTORREVERSE 12
#define BTNFORWARD 2
#define BTNREVERSE 3
#define BTNSTOP    4

void setup() {// put your setup code here, to run once
  pinMode (MOTORFORWARD, OUTPUT);     // for forward contactor shield
  pinMode (MOTORREVERSE, OUTPUT);     // for reverse contactor shield
  pinMode (BTNFORWARD, INPUT_PULLUP); // for forward push button
  pinMode (BTNREVERSE, INPUT_PULLUP); // for reverse push button
  pinMode (BTNSTOP, INPUT_PULLUP);    // for stop push button

} void loop() { // if forward pressed

  if (digitalRead(BTNFORWARD) == LOW) { // if forward pressed
    digitalWrite(MOTORREVERSE, LOW);   // stop reverse (just in case)
    digitalWrite(MOTORFORWARD, HIGH);  // go forward
  }
  if (digitalRead(BTNREVERSE) == LOW) {  // if reverse pressed
    digitalWrite(MOTORFORWARD, LOW);    // stop forward (just in case)
    digitalWrite(MOTORREVERSE, HIGH);   // go reverse

  }
  if (digitalRead(BTNSTOP) == LOW) {   // if stop pressed
    digitalWrite(MOTORFORWARD, LOW);   // stop forward
    digitalWrite(MOTORREVERSE, LOW);   // stop reverse

  }

Code for Photocell and Ultrasonic Sensors

//Date: 9/25/19
//Switch between Ultrasonic and Photocells Code

int readPin = 2; //Switch
#include <LiquidCrystal.h> //Load Liquid Crystal 

//Constants for Utlrasonic
LiquidCrystal LCD(12, 11, 5, 4, 3, 2);  //Create Liquid Crystal Object called LCD
int MOTOR = 1;   //MotorPin
int trigPin = 3; //Sensor Trip pin connected to Arduino pin 9
int echoPin = 4;  //Sensor Echo pin connected to Arduino pin 7
int servoControlPin = 5; //Servo control line is connected to pin 6

//Variables for Ultrasonic
int myCounter = 0;  //declare your variable myCounter and set to 0
float pingTime;  //time for ping to travel from the sensor to the target and return
float targetDistance; //Distance to Target in Centimeters
float speedOfSound = 776.5; //Speed of sound in miles per hour

//Constants for Photocells
const int pResistor1 = A0;
const int pResistor2 = A1;  // Photoresistor at Arduino analog pin A0
const int ledPin1 = 6;
const int ledPin2 = 7;      // Led pin at Arduino pin 6

//Variables for Photocells
int value1;
int value2; // Store value from photoresistor (0-1023)

void setup()
{
  Serial.begin(9600);
  pinMode(readPin, INPUT);
  pinMode(ledPin1, OUTPUT);    // Set lepPin - 6 pin as an output
  pinMode(pResistor1, INPUT);
  pinMode(ledPin2, OUTPUT);    // Set lepPin - 10 pin as an output
  pinMode(pResistor2, INPUT);  // Set pResistor - A0 pin as an input (optional)
  pinMode(MOTOR, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  LCD.begin(16, 2); //Tell Arduino to start your 16x2 LCD
  LCD.setCursor(0, 0); //Set LCD cursor to upper left corner, column 0, row 0
  LCD.print("Distance:");  //Print Message on First Row

}

boolean wayOne = true;
void loop() {
  if (digitalRead(readPin) == HIGH)
  {
    wayOne = !wayOne;
  }

  if (wayOne)  //Run Photocell Code
    PhotoCellCode();
  else
    UltrasonicCode();       //Run Ultrasonic Code

}
void PhotoCellCode() {
  value1 = analogRead(pResistor1);
  value2 = analogRead(pResistor2);

  Serial.print(value1);
  Serial.print(",");
  Serial.println(value2);
  Serial.print(",");

  //You can change value "25"
  if (value1 > 60) {
    digitalWrite(ledPin1, HIGH);
    //Turn led off//Turn led off
  }
  else {
    digitalWrite(ledPin1, LOW);
    //Turn led on

  }
  //You can change value "25"
  if (value2 > 750) {
    digitalWrite(ledPin2, HIGH);  //Turn led off//Turn led off
  }
  else {
    digitalWrite(ledPin2, LOW);
  }                              //Turn led on

}

void UltrasonicCode() {
  digitalWrite(trigPin, LOW); //Set trigger pin low
  delayMicroseconds(2000); //Let signal settle
  digitalWrite(trigPin, HIGH); //Set trigPin high
  delayMicroseconds(15); //Delay in high state
  digitalWrite(trigPin, LOW); //ping has now been sent
  delayMicroseconds(10); //Delay in high state

  pingTime = pulseIn(echoPin, HIGH);  //pingTime in microceconds
  pingTime = pingTime / 1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
  pingTime = pingTime / 3600; //convert pingtime to hours by dividing by 3600 (seconds in an hour)
  targetDistance = speedOfSound * pingTime; //This will be in miles, since we declared the speed of sound as kilometers per hour; although we're going to convert it back to centimeters
  targetDistance = targetDistance / 2; //Remember ping travels to the target and back from the target, so you must divide by 2 for actual target distance.
  targetDistance = targetDistance * 160934, 4; //Convert miles to centimeters by multipling by 160934,4



  LCD.setCursor(0, 1); //Set the cursor to the first column of the second row
  LCD.print("                "); //Print blanks to clear the row
  LCD.setCursor(0, 1);  //Set Cursor again to the first column of the second row
  LCD.print(targetDistance); //Print measured distance
  LCD.print(" centimeters");  //Print your units
  delay(250); //Pause to let things settle

  // Sensor Stop at a given Distance

  if ( targetDistance >= 10 )

  {
    digitalWrite(MOTOR, HIGH);
  }
  else
  {

    digitalWrite(MOTOR, LOW);
  }

  // Stop motor if too close


  // Prints the distance on the Serial Monitor

  Serial.print("Distance: ");
  Serial.println(targetDistance);

}

Right now I'm struggling with defining both loops for the sensors as push buttons rather than a "switch" between the two. Also, with combining the two codes into one where if the bin is full the motor will stop and wont turn back on until forward is pressed again.

Thanks for the tip! Definitely simplified things

Here's what I came up with, I'm wondering if it's necessary to include an interrupt statement (or something similar). So that if say, the Ultrasonic button is pushed, the Photocell loop will stop and vice versa.

//Date: 10/4/19
//Switch between Ultrasonic and Photocells Code w/ FWD RVRS STOP BUTTONS

#define MOTORFORWARD 13
#define MOTORREVERSE 12
#define BTNFORWARD 2
#define BTNREVERSE 3
#define BTNSTOP    4
#define BTNULTRASONIC  5
#define BTNPHOTOCELL    6
#include <LiquidCrystal.h>                //Load Liquid Crystal
int MOTOR = 7;                                 //MotorPin

//Constants for Utlrasonic
LiquidCrystal LCD(12, 11, 5, 4, 3, 2);    //Create Liquid Crystal Object called LCD
int trigPin = 8;                                    //Sensor Trip pin connected to Arduino pin 9
int echoPin = 9;                                  //Sensor Echo pin connected to Arduino pin 9
 
//Variables for Ultrasonic
int   myCounter = 0;                      //declare your variable myCounter and set to 0
float pingTime;                              //time for ping to travel from the sensor to the target and return
float targetDistance;                      //Distance to Target in Centimeters
float speedOfSound = 34300;         //Speed of sound in cm/s

//Constants for Photocells
const int pResistor1 = A0;
const int pResistor2 = A1;
const int pResistor3 = A2;              // Photoresistor at Arduino analog pins

//Variables for Photocells
int SensorValue1;
int SensorValue2;
int SensorValue3;                          // Store value from photoresistor (0-1023)

void setup()
{
  Serial.begin(9600);
  pinMode (MOTORFORWARD, OUTPUT);             // for forward contactor shield
  pinMode (MOTORREVERSE, OUTPUT);              // for reverse contactor shield
  pinMode (BTNFORWARD, INPUT_PULLUP);        // for forward push button
  pinMode (BTNREVERSE, INPUT_PULLUP);        // for reverse push button
  pinMode (BTNSTOP, INPUT_PULLUP);               // for stop push button
  pinMode (BTNPHOTOCELL, INPUT_PULLUP);     // for photocell button
  pinMode (BTNULTRASONIC, INPUT_PULLUP);    // for ultrasonic button
  pinMode (pResistor1, INPUT);
  pinMode (pResistor2, INPUT);
  pinMode (pResistor3, INPUT);
  pinMode (MOTOR, OUTPUT);
  pinMode (trigPin, OUTPUT);
  pinMode (echoPin, INPUT);
  LCD.begin(16, 2);
  LCD.setCursor(0, 0);
  LCD.print("Distance:");
}

void loop() {
  if (digitalRead(BTNFORWARD) == LOW) {     // if forward pressed
    digitalWrite(MOTORREVERSE, LOW);           // stop reverse (just in case)
    digitalWrite(MOTORFORWARD, HIGH);        // go forward
  }

  if (digitalRead(BTNREVERSE) == LOW) {     // if reverse pressed
    digitalWrite(MOTORFORWARD, LOW);        // stop forward (just in case)
    digitalWrite(MOTORREVERSE, HIGH);        // go reverse

  }
  if (digitalRead(BTNSTOP) == LOW) {          // if stop pressed
    digitalWrite(MOTORFORWARD, LOW);        // stop forward
    digitalWrite(MOTORREVERSE, LOW);         // stop reverse
  }
  if (digitalRead(BTNPHOTOCELL) == LOW) {   //if photocell button pressed
    PhotoCellCode();                                       //Run Photocell loop
  }

  if (digitalRead(BTNULTRASONIC) == LOW) {  //if ultrasonic button pressed
    UltrasonicCode();                                       //Run Ultrasonic loop
  }

}
void PhotoCellCode() {
  SensorValue1 = analogRead(pResistor1);                  //get the value from input pin 1
  SensorValue2 = analogRead(pResistor2);
  SensorValue3 = analogRead(pResistor3); 

  Serial.print(SensorValue1);
  Serial.print(",");
  Serial.print(SensorValue2);
  Serial.print(",");
  Serial.println(SensorValue3);                                       //print the value to Serial monitor
  delay(10);                                                                  //Set Delay in ms

  if (SensorValue1 < 300 && SensorValue2 < 300)            //if two are dark turn motor off
  {
    digitalWrite(MOTOR, LOW);
  }
  if (SensorValue1 < 300 && SensorValue3 < 300)
  {
    digitalWrite(MOTOR, LOW);
  }
  if (SensorValue2 < 300 && SensorValue3 < 300)
  {
    digitalWrite(MOTOR, LOW);
  }
  else
  {
    digitalWrite(MOTOR, HIGH);                         //else, keep the motor on
  }
}

void UltrasonicCode() {
  digitalWrite(trigPin, LOW);                           //Set trigger pin low
  delayMicroseconds(2000);                              //Let signal settle
  digitalWrite(trigPin, HIGH);                          //Set trigPin high
  delayMicroseconds(15);                                //Delay in high state
  digitalWrite(trigPin, LOW);                           //ping has now been sent
  delayMicroseconds(10);                                //Delay in high state

  pingTime = pulseIn(echoPin, HIGH);                    //pingTime in microceconds
  pingTime = pingTime / 1000000;                        //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
  targetDistance = speedOfSound * pingTime;       //Set targetdistance value (in cm)
  targetDistance = targetDistance / 2;                     
  LCD.setCursor(0, 1);                                         //Set the cursor to the first column of the second row
  LCD.print("                ");                                  //Print blanks to clear the row
  LCD.setCursor(0, 1);                                      //Set Cursor again to the first column of the second row
  LCD.print(targetDistance);                              //Print measured distance
  LCD.print(" centimeters");                                //Print your units
  delay(250);
  Serial.print("Distance: ");                              //Print Distance
  Serial.println(targetDistance);                         //Pause to let things settle

  if ( targetDistance >= 10 ) {                           // Stop motor if too close
    digitalWrite(MOTOR, HIGH);
  }
  else {
    digitalWrite(MOTOR, LOW);
  }
}