Hello all, I’m having issues with a project that myself and another classmate are working on. We are creating the sketch for a class project. the project is a crane that another student made and we have to provide the sketch and build a relay board. Our sketch consist of two main parts. 1st is a ultrasonic sensor to pickup when a person comes close to the crane. The 2nd part is the main program that controls the crane operation. The crane is operated automatically via timers, counters, and limit switches. the only human input is a start button. Our issue is that we cant seem to get the sketch to jump from the sequence that has the ultrasonic sensor to the main program. Any help would be greatly appreciated. It should be noted that this is the first major project either of us has worked on. We have done smaller projects and have a decent handle on coding, but not a prolific one.
#include <LiquidCrystal.h>
#define trigger 52
#define echo 53
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
float time = 0, distance = 0;
const int buttonPin = 8;
const int ledPin = 12;
// variables will change:
int sbut = 22;
int cntr = 23;
int dwnlim = 40;
int leftlim = 25;
int lift = 26;
int crot = 27;
int ccrot = 28;
int dwn = 29;
int elecmag = 30;
int sbutstate = 0;
int buttonPushCounter = 0;//****************NA
int buttonState = 0;//**********************NA
int lastButtonState = 0;//******************NA
int bpoll = 0;//****************************NA
int old = 0;//******************************NA
int state = 0;//****************************NA
int crotstate = 0;
int dwnlimstate = 0;
int lastdwnlimstate = 0;
int cntrstate = 0;
int lastcntrstate = 0;
int dententcounter = 0;
void setup()
{
lcd.begin(16, 2);
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
lcd.setCursor(1, 0);
lcd.print(“ARDUINO CRANE”);
lcd.setCursor(2, 1);
lcd.print(“Created by”);
delay(2000);
lcd.clear();
lcd.setCursor(2, 0);
lcd.print(“Oliver”);
lcd.setCursor(2, 1);
lcd.print(“Roy”);
delay(2000);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(sbut, INPUT);
pinMode(cntr, INPUT);
pinMode(dwnlim, INPUT);
pinMode(leftlim, INPUT);
pinMode(lift, OUTPUT);
pinMode(crot, OUTPUT);
pinMode(ccrot, OUTPUT);
pinMode(dwn, OUTPUT);
pinMode(elecmag, OUTPUT);
}
void loop()
{
lcd.clear();
digitalWrite(trigger, LOW);
delayMicroseconds(2);
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);
delayMicroseconds(2);
time = pulseIn(echo, HIGH);
distance = time * 340 / 20000;
lcd.setCursor(1, 0);
lcd.print(“ARDUINO CRANE”);
delay(1000);
if (distance < 15)
{
lcd.clear();
lcd.setCursor(4, 0);
lcd.write(“Welcome!”);
delay(3000);
lcd.clear();
lcd.setCursor(3, 0);
lcd.print(“This is an”);
lcd.setCursor(0, 1);
lcd.print(“electromagnaetic”);
delay(2000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print(“crane.”);
lcd.setCursor(1, 1);
lcd.print(“The crane will”);
delay(2000);
lcd.clear();
lcd.print(“move each of the”);
lcd.setCursor(1, 1);
lcd.print(“the three cans”);
delay(2000);
lcd.clear();
lcd.print(“from their home”);
lcd.setCursor(2, 1);
lcd.print(“position to”);
delay(2000);
lcd.clear();
lcd.setCursor(3, 0);
lcd.print(“their new”);
lcd.setCursor(3, 1);
lcd.print(“designated”);
delay(2000);
lcd.clear();
lcd.setCursor(4, 0);
lcd.print(“locations.”);
lcd.setCursor(0, 1);
lcd.print(“Then, the crane”);
delay(2000);
lcd.clear();
lcd.print(“will return each”);
lcd.setCursor(0, 1);
lcd.print(“of the cans back”);
delay(2000);
lcd.clear();
lcd.setCursor(3, 0);
lcd.print(“to the home”);
lcd.setCursor(4, 1);
lcd.print(“position.”);
delay(2000);
lcd.clear();
lcd.print(“To begin, push”);
lcd.setCursor(0, 1);
lcd.print(“the Red Button”);
delay(1000);
loop(); {
sbutstate = digitalRead(sbut); //<<start button/redbutton
digitalWrite(elecmag, HIGH);
switch (sbutstate) {
case 1:
digitalWrite(lift, HIGH);
delay(1200);
digitalWrite(crot, HIGH);
case 0:
digitalWrite( lift, LOW);
break;
}
cntrstate = digitalRead(cntr);
// compare the buttonState to its previous state
if (cntrstate != lastcntrstate) {
// if the state has changed, increment the counter
if (cntrstate == HIGH) {
// if the current state is HIGH then the button went from off to on:
dententcounter++;
delay(50);
}
// save the current state as the last state, for next time through the loop
lastcntrstate = cntrstate;
// turns off base rotation motor after four activations of the counter switch by checking the modulo of the
// counter. the modulo function gives you the remainder of the
// division of two numbers:
if (dententcounter % 4 == 0) {
digitalWrite(crot, LOW );
delay(250);
} else {
digitalWrite(crot, HIGH);
} loop(); {
dwnlimstate = digitalRead(dwnlim);
if (dwnlimstate == HIGH) { // if down limit is high(not activated) then turn on down motor
digitalWrite(dwn, HIGH);
digitalWrite(elecmag, LOW);
delay (500);
} exit;
}
}
dwnlimstate = digitalRead(dwnlim);//checks down limit for activation. if activated cuts off down motor.
if (dwnlimstate == LOW) {
digitalWrite(dwn, LOW);
}
else {
digitalWrite(dwn, HIGH);
}
dwnlimstate = digitalRead(dwnlim);// not sure if this step works yet:-)
switch (sbutstate) {
case 1:
digitalWrite(lift, HIGH);
delay(1200);
digitalWrite(crot, HIGH);
case 0:
digitalWrite( lift, LOW);
break;
}
}
}
}