dysfunctional function

I am trying to assemble code from a number of sketches that were correct, but when assigned to individual functions, they are not responding to sensor input. The servos, for example, were moving randomly when the sonar value was less than 70 in the original sketch, but now, when called in the loop, they only move for a short time, then come to a full stop, totally ignoring the "if (sonarVal < 70)" statement. Also, only a few values appear in the serial monitor. Any suggestions?

#include <Servo.h> 
#include <Stepper.h>

  int leftSwitchPin = 2;
  int rightSwitchPin = 3;
  int buttonState1 = 0;
  int buttonState2 = 0;
 
  const int stepsPerRevolution1 = 100; 
  Stepper myStepper1(stepsPerRevolution1, 44,42,40,38);
  int stepCount = 0;
  
 
  const int stepsPerRevolution2 = 100; 
  Stepper myStepper2(stepsPerRevolution2, 53,51,49,47);
 
 //LED's
int ledLeft = 35;
int ledMiddle = 36;
int ledRight = 37;
int ledFrontRow [10] = {20,21,22,23,24,25,26,27,28,29};
//Motors
int motorLeft = 29; //dc motors
int motorMiddle = 31;
int motorRight = 33;
//Servo
Servo myservo1;
Servo myservo2;
Servo myservo3;

unsigned int duration = 0;              

int lasttime1 = 0;
int lasttime2 = 0; 
int randstart = 0; 
int randend = 0;

int pos1 = 0;
int pos2 = 0;
int pos3 = 0;

int sonarPin = A1;
int sonarVal =0;

void setup() 
{
  pinMode (leftSwitchPin,INPUT);               //shift to pin 2
  pinMode (rightSwitchPin,INPUT);               //shift to pin 3
  
  myStepper1.setSpeed(30);
  myStepper2.setSpeed(30);
  
 pinMode(motorLeft, OUTPUT);  //dc motors
 pinMode(motorMiddle, OUTPUT);
 pinMode(motorRight, OUTPUT); 

 pinMode(sonarVal,INPUT);

  myservo1.attach(9); 
  myservo2.attach(10);
  myservo3.attach(11);
  
  Serial.begin(9600);
  pinMode(sonarPin,INPUT);  
} 
void loop ()
{
 sonarVal = analogRead (sonarPin);
 Serial.println (sonarVal);
 delay(500);
 if (sonarVal < 70)
 {
  servoRandom();
  motor();
  unipolarStepper();
  bipolarStepper();
   }
}
 void servoRandom() 
 {
   
   sonarVal = sonarVal *2; ///5
   Serial.println(sonarVal);
   delay(250);//50

   lasttime1 = millis();
   randstart =  random(554, 1054); 
   randend =  random(500, 2400);

  while(pos1 <= randend)  
  {     
    pos1 += 1;
    myservo1.writeMicroseconds(pos1);   
    delay(2);   //2                 
  } 
    while(pos2 <= randend)  
  {     
    pos2 += 1;
    myservo2.writeMicroseconds(pos2); 
    delay(2);   //2                 
  }
     while(pos3 <= randend)  
  { 
    pos3 += 1;
    myservo3.writeMicroseconds(pos3);    
    delay(2);   //2                 
  }
    duration = millis() - lasttime1;
    lasttime2 = millis();
  
  while(pos1 >= randstart)   
  {                                
    pos1-=1;
    myservo1.write(pos1);   
    delay(2);                      
  } 
    while(pos2 >= randstart)   
  {                                
    pos2-=1;
    myservo2.write(pos2);   
    delay(2);                      
  }
    while(pos3 >= randstart)   
  {                                
    pos3-=1;
    myservo3.write(pos3);   
    delay(2);                      
  }
  duration = millis() - lasttime2; 
  duration = millis() - lasttime1;
  }
 void motor ()
  {
 
   digitalWrite (motorLeft, HIGH);
   digitalWrite (motorMiddle,HIGH);
   digitalWrite (motorRight, HIGH);
 
   
  }

  
   void unipolarStepper() 
{

  while (digitalRead(rightSwitchPin) == LOW) //keep going until limit switch reached
  {
    myStepper1.step(1);  //move right
  }

  while (digitalRead(leftSwitchPin) == LOW) //keep going until limit switch reached
  {
    myStepper1.step(-1);  //move left
    Serial.println (leftSwitchPin);
  }
}
void bipolarStepper()
{
 // step one step:
  myStepper2.step(1);
 //Serial.print("steps:" );
  //Serial.println(stepCount);
  stepCount++;
  delay(500);
}

What exactly is attached to the sonarPin ?
What values do you get for sonarVal in loop() ?

A Maxsonar sensor is attached to the pin. Readings range from 150 when standing at about six feet, to under 50 at three feet. The sensor was functioning well when included in a sketch for the servos but now, only a few values are seen on the serial monitor before all comes to a stop.

Here is the specific part of the sketch that is most troubling. Is this the correct way to call functions within a loop?

void loop ()
{
 sonarVal = analogRead (sonarPin);
 Serial.println (sonarVal);
 delay(500);
 if (sonarVal < 70)
 {
  servoRandom();
  motor();
  unipolarStepper();
  bipolarStepper();
   }
}

Yes, that is perfectly valid code.

The four functions

  servoRandom();
  motor();
  unipolarStepper();
  bipolarStepper();

will be called in sequence.

Looking at the code, servoRandom should move the servos in sequence first to one location, then another. Does this happen?
The motor should then turn on in a certain way - does this happen?
After that the unipolar stepper should sweep to both extremes. Does this happen?
Finally the bipolar stepper should step once. Again, does this happen?

Yes, the servos do move but only momentarily. Previously they would continue moving until the viewer moved a distance away, then stopped. Everything seems to be happening just one time rather than continuously while the sensor value was within range.

The motor runs without stopping, regardless of sensor readings.

I was working on the stepper drivers when these other calamities occurred. I'd like to resolve them before connecting the steppers.

Why do you believe the serial monitor displays only a few values, then stop?

ifugaopapercraft:
Yes, the servos do move but only momentarily. Previously they would continue moving until the viewer moved a distance away, then stopped. Everything seems to be happening just one time rather than continuously while the sensor value was within range.

The motor runs without stopping, regardless of sensor readings.

I was working on the stepper drivers when these other calamities occurred. I'd like to resolve them before connecting the steppers.

Why do you believe the serial monitor displays only a few values, then stop?

Because something is not working :slight_smile:

I would suggest commenting out three of the four functions in the if and tackling them one at a time.

I reiterate my note about the functions running in sequence. First the servos will twitch, then stop. Then the motor will start. Then the unipolar stepper will do a sweep, and stop. Finally the bipolar stepper will step once, and once only. Next time through the loop the sequence will start again (but as the motor's already running, it won't start running again).

Seem to have a pin conflict!

pin 29 ledFrontRow[]
pin 29 motorLeft

Not that you are currently using the array 'ledFrontRow'

You seem to have quite a lot of hardware connected up. It may be that your power supply isn't up to the job or your Arduino is being reset due to electrical noise. It may also be that your limit switches aren't working correctly and you're getting stuck in a loop in unipolarStepper(). I suggest you follow majenko's advice and disable parts of your sketch to see which part is triggering the problem, that will tell you which part of the circuit to investigate.

Thanks for the advice! Firstly, I certainly didn't intend each function to run in sequence as you describe. Rather, each should occur simultaneously. The servos, motors, and steppers all should fire at the same time while the sensor value is less than 70. Does this mean that calling the functions within the loop is an altogether wrong method?

As for power, I have an 8 amp, 12v supply. I'll disable the functions, one by one as you suggest.

  while (digitalRead(leftSwitchPin) == LOW) //keep going until limit switch reached
  {
    myStepper1.step(-1);  //move left
    Serial.println (leftSwitchPin);
  }

Are you expecting the pin number to magically change in the middle of the while loop?

ifugaopapercraft:
Thanks for the advice! Firstly, I certainly didn't intend each function to run in sequence as you describe. Rather, each should occur simultaneously. The servos, motors, and steppers all should fire at the same time while the sensor value is less than 70. Does this mean that calling the functions within the loop is an altogether wrong method?

As for power, I have an 8 amp, 12v supply. I'll disable the functions, one by one as you suggest.

To get everything to run concurrently, you will need to implement one or more Finite State Machines. Personally, because all the motors and things are doing such radically different actions, I'd probably create one separate FSM for each device, with the sensor triggering the start / end of them all at once.

int sonarVal =0;

void setup() 
{
  pinMode (leftSwitchPin,INPUT);               //shift to pin 2
  pinMode (rightSwitchPin,INPUT);               //shift to pin 3
  
  myStepper1.setSpeed(30);
  myStepper2.setSpeed(30);
  
 pinMode(motorLeft, OUTPUT);  //dc motors
 pinMode(motorMiddle, OUTPUT);
 pinMode(motorRight, OUTPUT); 

 pinMode(sonarVal,INPUT);

  myservo1.attach(9); 
  myservo2.attach(10);
  myservo3.attach(11);
  
  Serial.begin(9600);
  pinMode(sonarPin,INPUT);  
} 
void loop ()
{
 sonarVal = analogRead (sonarPin);
 Serial.println (sonarVal);
 delay(500);
 if (sonarVal < 70)

Is this correct? Assigning sonarVal as an input pin (0) then using it to store a variable?

Assigning sonarVal as an input pin (0) then using it to store a variable?

That code is NOT assigning sonarVal as an input pin. sonarPin is assigned as an input pin. The value read from that pin is assigned to sonarVal.

What kind of sonar is it? Does it really return an analog value?

Is this correct? Assigning sonarVal as an input pin (0) then using it to store a variable?

No. You are confusing yourself (and me)

A variable with a name like sonarVal would normally hold a value whilst one like sonarPin would hold a pin number. There is nothing sacrosanct about this but it helps when understanding code.

You have set sonarVal to zero, defined it as in INPUT (why ?) then try to read from the sonarPin which has not been defined but which you have tried to set as an INPUT.

Review the pin(s) that you are using and the variable(s) that hold value(s) read from sensor(s)

I've never attempted anything nearly as complex as this project so your collective advise is invaluable. Being a sculptor of traditional media, the leap into Arduino World is quite a challenge. I'll no doubt stumble through your suggestions and post again. Thanks.

Keith