Hello excellent and very smart people.
This is the most extensive code I've written by myself, so please bear with me.
Basically, I've got two switches, I flick one, it turns on a few relays to control a solenoid, and reads an ultrasonic sensor, which displays to an LCD screen.
I flick the other switch, and it does the same, but with another ultrasonic sensor, and other set of relays.
This is my code (sorry if you're spotting lots of errors, but it complies and mostly works).
#include <LiquidCrystal.h>
const int rs = 2, en = 1, d4 = 3, d5 = 4, d6 = 5, d7 = 6;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int righttriggerPin = 9; //Define IO pins
int rightechoPin = 10;
int lefttriggerPin = 11; //Define IO pins
int leftechoPin = 12;
long rightduration;
double rightdistance;
long leftduration;
double leftdistance;
const int rightbut = 24;
const int leftbut = 26;
int shutbutstate = 0;
int rightbutstate = 0;
int leftbutstate = 0;
const int relay1 = 31;
const int relay2 = 33;
const int relay3 = 35;
const int relay4 = 37;
const int relay5 = 39;
const int relay6 = 41;
const int relay7 = 43;
const int relay8 = 45;
void setup()
{
pinMode(lefttriggerPin, OUTPUT); //Define pin
pinMode(leftechoPin, INPUT);
pinMode(righttriggerPin, OUTPUT); //Define pin
pinMode(rightechoPin, INPUT);
lcd.begin(16,2); //Start the LCD screen, define the number of characters and rows
lcd.clear(); //Clear the screen
lcd.setCursor(0,0); //Set the cursor to first character, first row
lcd.print("Wood Distance:"); //Display this text
pinMode (rightbut, INPUT);
pinMode (leftbut, INPUT);
pinMode (relay1, OUTPUT);
pinMode (relay2, OUTPUT);
pinMode (relay3, OUTPUT);
pinMode (relay4, OUTPUT);
pinMode (relay5, OUTPUT);
pinMode (relay6, OUTPUT);
pinMode (relay7, OUTPUT);
pinMode (relay8, OUTPUT);
}
void loop()
{
rightbutstate = digitalRead(rightbut);
if (rightbutstate == HIGH) {
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay7, HIGH);
digitalWrite(relay8, HIGH);
delay (1000);
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay7, LOW);
digitalWrite(relay8, LOW);
delay (1000);
//how do I make it only run the above bit once
digitalWrite(righttriggerPin, LOW); //Reset the trigger pin
delay(1000);
digitalWrite(righttriggerPin, HIGH); //Create a 10 micro second pulse
delayMicroseconds(10);
digitalWrite(righttriggerPin, LOW);
rightduration = pulseIn(rightechoPin, HIGH); //Read the pulse travel time in microseconds.
rightdistance= rightduration*0.034/2; //Calculate the distance - speed of sound is 0.034 cm per microsecond
lcd.setCursor(0,1); //Set cursor to column 0, row 1
lcd.print(rightdistance*10); //Display the distance
lcd.print(" mm");
}
else {
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
delay (1000);
digitalWrite(relay3, LOW);
digitalWrite(relay4, LOW);
delay (1000);
lcd.setCursor(0,1); //Set cursor to column 0, row 1
lcd.print("Push Button"); //Display the distance
}
leftbutstate = digitalRead(leftbut);
if (leftbutstate == HIGH) {
digitalWrite(relay6, HIGH);
digitalWrite(relay5, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
delay (1000);
digitalWrite(relay6, LOW);
digitalWrite(relay5, LOW);
digitalWrite(relay3, LOW);
digitalWrite(relay4, LOW);
delay (1000);
//how do I stop it after here?
digitalWrite(lefttriggerPin, LOW); //Reset the trigger pin
delay(1000);
digitalWrite(lefttriggerPin, HIGH); //Create a 10 micro second pulse
delayMicroseconds(10);
digitalWrite(lefttriggerPin, LOW);
leftduration = pulseIn(leftechoPin, HIGH); //Read the pulse travel time in microseconds.
leftdistance= leftduration*0.034/2; //Calculate the distance - speed of sound is 0.034 cm per microsecond
lcd.setCursor(0,1); //Set cursor to column 0, row 1
lcd.print(leftdistance*10); //Display the distance
lcd.print(" mm");
}
else {
digitalWrite(relay5, HIGH);
digitalWrite(relay6, HIGH);
delay (1000);
digitalWrite(relay7, LOW);
digitalWrite(relay8, LOW);
lcd.setCursor(0,1); //Set cursor to column 0, row 1
lcd.print("Push Button"); //Display the distance
}
}
The problem I'm running into, is that I only want the solenoids to cycle once when the switch is flicked (they continue to run atm), then for the ultrasonic sensor to continue running until it's turned off, then stop, and stop displaying to the lcd screen so if I flick the other switch, it'll display the other ultrasonic sensor on it.
ATM, the LCD screen/relays is getting conflicted with the other 'if' statement, and I'm a little stuck.
Basically what I'm trying to make is an ultrasonic sensor to use on a miter saw station, flick a switch, it pops out, and displays the distance to the wood from the sensor (which will tell me how long the wood is with some maths, and I can make accurate cuts).
And my tangle of mess, it does make sense, and I have distance required to go to the spots it needs to go to.
Car solenoids are cheap and easy.
Even taught myself how to attach pins to make things neater and easier.
Thanks in advance, I'm sure it's annoying constantly pointing out what's obvious with newbies sketches.
I tried to put the if statement in the setup, but it popped up with errors, so I don't think that works there.