Go Down

Topic: "pulling man" installation ( ball goes up and down based on servo time (Read 264 times) previous topic - next topic

iejun

Hi all,

Wondering if this i a good setup for 1 ball going up and down based on given time. I want servo in the For loops of the pattern to be rotating based on time in Milliseconds..not sure if its correct this way.

Als am jumping from each void to another one by just using an variable...is this correct of is there an much easier way to do.

kind regards,
ian



Code: [Select]
#include <Ultrasonic.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x40); // board 0
//Adafruit_PWMServoDriver pwm2 = Adafruit_PWMServoDriver(0x41); // board 1
//Adafruit_PWMServoDriver pwm3 = Adafruit_PWMServoDriver(0x42); // board 2
//Adafruit_PWMServoDriver pwm4 = Adafruit_PWMServoDriver(0x43); // board 3
Ultrasonic ultrasonic(13, 12);

int distance;
int route = 0;
int speedup = 350;
int speeddown = 325;
int speedstop = 338;
int speedboard0;
long p01 = millis();

void setup() {
  Serial.begin(9600);
  pwm1.begin();
  pwm1.setPWMFreq(60);
  // pwm2.begin();
  // pwm2.setPWMFreq(60);
  // pwm3.begin();
  // pwm3.setPWMFreq(60);
  // pwm4.begin();
  // pwm4.setPWMFreq(60);
}
void loop() {
  sensor();
  calibrate();
  pattern01();
}

void servoTest() {
  //pwm1.setPWM(1, 0, 338); // stopping point
  //pwm1.setPWM(0, 0, 338);
  //pwm1.setPWM(0, 0, distance * 10);
  //pwm1.setPWM(0, 0, speedboard0);
}

void sensor() {
  distance = ultrasonic.Ranging(CM);
  Serial.print(distance * 10);
  Serial.print(" servo speed ");
  Serial.print("    ");
  Serial.print(ultrasonic.Ranging(CM));
  Serial.println(" cm afstand");
  delay(100);
}

// spin up servo until ball is in range of minus 10cm of sensor
void calibrate() {
  if (route == 0) {
    speedboard0 = speedup;
    pwm1.setPWM(0, 0, speedboard0);
    if (distance < 10) {

      pwm1.setPWM(0, 0, speedstop);
      delay(2000);
      route = 1; //show pattern01
    }
  }
}
void pattern01() {
  if (route == 1) {
    speedboard0 = speeddown;
    for (p01 = 0; p01 < 200; p01++) {
      Serial.print("patroon 01 start");
      Serial.print("  ");
      Serial.println(p01);
      pwm1.setPWM(0, 0, speedboard0);
    }
    pwm1.setPWM(0, 0, speedstop);
    delay(2000);
    route = 2; //show pattern02
  }
}
void pattern02() {
 if (route == 2) {
    speedboard0 = speeddown;
    for (p01 = 0; p01 < 100; p01++) {
      Serial.print("patroon 01 start");
      Serial.print("  ");
      Serial.println(p01);
      pwm1.setPWM(0, 0, speedboard0);
    }
    pwm1.setPWM(0, 0, speedstop);
    delay(2000);
    route = 0; //go gack to calibrate
  }
}

pert

Als am jumping from each void to another
The correct term is function. void is the return type, in this case indicating that the function doesn't return anything.

am jumping from each void to another one by just using an variable...is this correct of is there an much easier way to do.
The concept is ok but pattern02() will never run because you don't call it and thus the code in the other two functions will only ever be run once.

Wondering if this i a good setup for 1 ball going up and down based on given time. I want servo in the For loops of the pattern to be rotating based on time in Milliseconds..not sure if its correct this way.
It's not very clear to me what you're trying to do but the use of delay() is likely to cause you problems. You probably want to be constantly reading the sensor, not with multiple seconds of delay between readings. I suggest you study File > Examples > 02.Digital > BlinkWithoutDelay and the accompanying tutorial:
https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
To see how you can write your code without any use of delay().


Go Up