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.