Can't Escape

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;
}
}
}
}

for starters, read the sticky post at the top of the forum on how to use the forum, pay special attention to the part about how to post the code using code tags. Also a wiring diagram would help - can be a hand drawn sketch is fine

1.) Get rid of ALL the delays (blink without delay example)
2.) Look up "software state machines"
3.) Get rid of the LCD for now and use Serial.println() calls to debug things - simple systems are easier to debug and getting rid of the LCD takes away some unnecessary complexity
4.) Get the ultrasonic sensor working by itself
5.) Get the crane working by itself, then
6.) Trigger the crane part based off distance as reported by the ultrasonic part of the code

beardenr:

void loop()

{
// code removed
 
  loop(); {
// code removed
  }
// code removed

Doesn’t look like this is even going to compile.
The messy formatting (do a ctrl-T in the IDE) is not helping that assessment, of course.

NEVER call the loop() function.

 }   loop(); {

That's like a snake swallowing its own tail.

The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R
Planning and Implementing a Program

Yo beardenr,

Read Robin2's footnote link: Planning and Implementing a Program. This is the most important common link here if you are writing software for Arduino.

I can certainly understand the desire to make it work, even if just clumbsly. and not having to do a lot of research and study to make it work like a more properly written program.

take a few minutes and a pencil. write out each step, do not use

start sequence - detect if human is present, ( I did not see the call to the ultrasonic…)
display the welcome text
set flag that human presence has been detected

now that a human has been detected, run the program

I assume that the program runs the crane. you should be able to to these steps manual.

press the button
drop the bucket
raise the bucket
that should work, once it does remark that out for now
then add the next thing

press the button
rotate crane to stop
rotate crane back

once that works…

press the button
pick up the thing

you get the idea. make one thing work, keep adding
as a note, each action, drop bucket, rotate, lift bucket, etc, should be complete and you should not go back to add things.
this looks like where you got in trouble.

once you can get the crane to do the dance with one button press, you are almost there.

as you can see, I moved your screen print to void starting()
as was pointed out, you should never call loop()
and you should never have two of the same names.

another tip use some end markers

} // =========== end of if (drop_bucket

things like that. your current program is not segmented properly
this will be corrected when you do each movement one at a time.
you have more than one if(xxx) { with closing bracket at the end of the sketch

a note about delay() it is considdered poor practice to use delay()
but, since you have a demonstration device that only does one thing at a time
the need to use ‘blink without delay’ is not critical to change that to make this work.

at this stage, get each action working
one at a time.
then make them work in sequence

I can see you have an issue with how to keep things working.

if (button_pressed == 0 ) { // prevents second button press while in operation
if (button == high)
button_pressed = 1; // sets the flag
}
}

lift crane
rotate crane
pick up
rotate
drop
return home
then set button_pressed = 0

see how the flag puts you into that ‘in-progress’ state ?
this may be the answer to the question you originally asked.

this code still does not work, but I added some notes

// =========== end of some sequence. etc

and moved the initial screen print to a different function

#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) {
    starting();
  }


  // ============  end starting

  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);
    }
  } //  added to end
    // save the current state as the last state, for next time through the loop
    lastcntrstate = cntrstate;
// ==============   end of if 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(); {    this is call #2....  

    
      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;
  }
}  // end  void loop

void starting() {   // +++++++++++++++++  sense people  ++++
  {

    {
      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);
    }