Go Down

Topic: dysfunctional function (Read 744 times) previous topic - next topic

ifugaopapercraft

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?


Code: [Select]
#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);
}

 

UKHeliBob

What exactly is attached to the sonarPin ?
What values do you get for sonarVal in loop() ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

ifugaopapercraft

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.

ifugaopapercraft

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

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

majenko

Yes, that is perfectly valid code.

The four functions
Code: [Select]

  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?
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Go Up