Recent Posts

Pages: [1] 2 3 ... 10
1
This is the code which tries to use a potentiometer to control the desired angle, without the compass.

Code: [Select]
#include <Servo.h>            //Servo library

//PID angle control for Parallax 360 Servo
Servo servo_test;        //initialize a servo object for the connected servo 
int pinFeedback = 5;
               
int target_angle;
float tHigh = 0;
float tLow = 0;
int tCycle = 0;
float dc = 0;
float angle = 0; //Measured angle from feedback
float dcMin = 2.9; //From Parallax spec sheet
float dcMax = 97.1; //From Parallax spec sheet
float Kp = .7; //Proportional Gain, higher values for faster response, higher values contribute to overshoot.
float Ki = .2; //Integral Gain, higher values to converge faster to zero error, higher values produce oscillations. Higher values are more unstable near a target_angle = 0.
float iLimit = 5; //Arbitrary Anti-wind-up
float Kd = 1; //Derivative Gain, higher values dampen oscillations around target_angle. Higher values produce more holding state jitter. May need filter for error noise.
float prev_error = 0;
float prev_pError = 0;
float error = 0;
float pError = 0;
float iError = 0;
int potpin = 3;



 
void setup()
{
  Serial.begin (9600);
  servo_test.attach(6);      // attach the signal pin of servo to pin9 of arduino
  pinMode(5, INPUT);
}
 
void loop()
{

 target_angle = analogRead(potpin);
 target_angle = map(target_angle,0,359,0,359);
 
 
 
  while(1) //From Parallax spec sheet
  {
    tHigh = pulseIn(pinFeedback, HIGH);
    tLow = pulseIn(pinFeedback, LOW);
    tCycle = tHigh + tLow;
    if ( tCycle > 1000 && tCycle < 1200)
    {
      break; //valid tCycle;
    }
  }

 
  dc = (100 * tHigh) / tCycle; //From Parallax spec sheet, you are trying to determine the percentage of the HIGH in the pulse
 
  angle = ((dc - dcMin) * 360) / (dcMax - dcMin + 1); //From Parallax spec sheet

  //Keep measured angles within bounds
  if (angle < 0)
  {
    angle = 0;
  }
  else if(angle > 359)
  {
    angle = 359;
  }

  if (target_angle < 0)
  {
    target_angle = 360 + target_angle; //handles negative target_angles;
  }
 
  error = target_angle - angle;
 
  if(error > 180)
  {
    error = error - 360; //tells it to rotate in the other direction because it is a smaller angle that way.
  }
  if (error < -180)
  {
    error = 360 - error - 360; //tells it to rotate in the other direction because it is a smaller angle that way.
  }

  // PID controller stuff, Adjust values of Kp, Ki, and Kd above to tune your system
  float pError = Kp * error;
  float iError = Ki * (error + prev_error);

  if  (iError > iLimit)
  {
    iError = iLimit;
  }
  if (iError <  -iLimit)
  {
    iError = -iLimit;
  }
 
  prev_error = error;
  float dError = Kd * (pError - prev_pError);
  prev_pError = pError;

  error = error / 2; //max 180 error will have max 90 offset value

  int val = 93 - (Kp * error) - iError - dError; // 93 is the middle of my servo's "no motion" dead-band
 
  servo_test.write(val); //Move the servo

  Serial.println(val);
 

  }







This is the source code which I'm trying to work off, credit to @JavelinPoint, in this code if you adjust the target angle between 0 - 359 the servo will accurately move / point to that position.

Code: [Select]
#include <Servo.h>            //Servo library

//PID angle control for Parallax 360 Servo
Servo servo_test;        //initialize a servo object for the connected servo 
int pinFeedback = 5;
               
int target_angle = 180;
float tHigh = 0;
float tLow = 0;
int tCycle = 0;
float dc = 0;
float angle = 0; //Measured angle from feedback
float dcMin = 2.9; //From Parallax spec sheet
float dcMax = 97.1; //From Parallax spec sheet
float Kp = .7; //Proportional Gain, higher values for faster response, higher values contribute to overshoot.
float Ki = .2; //Integral Gain, higher values to converge faster to zero error, higher values produce oscillations. Higher values are more unstable near a target_angle = 0.
float iLimit = 5; //Arbitrary Anti-wind-up
float Kd = 1; //Derivative Gain, higher values dampen oscillations around target_angle. Higher values produce more holding state jitter. May need filter for error noise.
float prev_error = 0;
float prev_pError = 0;
float error = 0;
float pError = 0;
float iError = 0;

 
void setup()
{
  Serial.begin (9600);
  servo_test.attach(6);      // attach the signal pin of servo to pin9 of arduino
  pinMode(5, INPUT);
}
 
void loop()
{
  //use line below to determine "center" of servo. When near values of 90, manually force servo by hand in both directions to see if it continues to turn.
  //servo_test.write(85); //clockwise: max 0 - 90 min, counter-clockwise: min 96-180 max, may be different for your servo, 93 stopped.
 
  while(1) //From Parallax spec sheet
  {
    tHigh = pulseIn(pinFeedback, HIGH);
    tLow = pulseIn(pinFeedback, LOW);
    tCycle = tHigh + tLow;
    if ( tCycle > 1000 && tCycle < 1200)
    {
      break; //valid tCycle;
    }
  }
 
  dc = (100 * tHigh) / tCycle; //From Parallax spec sheet, you are trying to determine the percentage of the HIGH in the pulse
 
  angle = ((dc - dcMin) * 360) / (dcMax - dcMin + 1); //From Parallax spec sheet

  //Keep measured angles within bounds
  if (angle < 0)
  {
    angle = 0;
  }
  else if(angle > 359)
  {
    angle = 359;
  }

  if (target_angle < 0)
  {
    target_angle = 360 + target_angle; //handles negative target_angles;
  }
 
  error = target_angle - angle;
 
  if(error > 180)
  {
    error = error - 360; //tells it to rotate in the other direction because it is a smaller angle that way.
  }
  if (error < -180)
  {
    error = 360 - error - 360; //tells it to rotate in the other direction because it is a smaller angle that way.
  }

  // PID controller stuff, Adjust values of Kp, Ki, and Kd above to tune your system
  float pError = Kp * error;
  float iError = Ki * (error + prev_error);

  if  (iError > iLimit)
  {
    iError = iLimit;
  }
  if (iError <  -iLimit)
  {
    iError = -iLimit;
  }
 
  prev_error = error;
  float dError = Kd * (pError - prev_pError);
  prev_pError = pError;

  error = error / 2; //max 180 error will have max 90 offset value

  int val = 93 - (Kp * error) - iError - dError; // 93 is the middle of my servo's "no motion" dead-band
 
  servo_test.write(val); //Move the servo

  Serial.println(val);


 
}




The desired behavior I'm trying to achieve is to have the target angle determined by the compass readings and have it point in that direction, whats currently happening is the motor will make a few slight turns (no specific position) and then begin to randomly spin continuously  without stopping.
2
Definitely this time after running it I will test how hot it is running and measure how much current each regulator is drawing. Right now this sounds like the most likely culprit.
3
I got this error. I changed the sketchbook location or the folder which I am using to some other location. Now it is working properly without any error.
4
Programming Questions / Re: LCD problem
Last post by gfvalvo - Today at 03:54 am
Some processors support it.
5
Hardware SPI, as used by the SPI library, is only available on specific pins. That's hardwired into the microcontroller. You can't just chose to do hardware SPI on any given pins. It is possible to do software SPI on any pins.
6
Hello! So I am currently working on a project to combine a load cell, ultrasound sensor and a peristaltic pump to dispense some water until the glass reaches a certain weight. Individually, all three components are working fine and I have been able to integrate the ultrasound sensor and the pump without any problems however, when I try to integrate all 3, I seem to face a problem. The tare function in my void setup is somehow not executing and as a consequence, the serial monitor is stuck at the code before the tare function. I am wondering if this is a programming error and if so, why is it not working?

Code: [Select]
//author of ultrasound code: www.elegoo.com
//author of load code: Nathan Seidle

#include "SR04.h"
#define TRIG_PIN 12
#define ECHO_PIN 11
#include "HX711.h"
#define RST 4
#define DOUT  3
#define CLK  2
HX711 scale(DOUT, CLK);
float calibration_factor = -2680.00; //-7050 worked for my 440lb max scale setup
float oldCal_factor;
float oldWeight;
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long petriDish;
int pushButton = 2;
int motorControl = 9;
void setup() {
  Serial.begin(9600);
  Serial.println("test line");// wrote these testlines to see which part of my setup is holding up the program
  pinMode(pushButton, INPUT); 
  pinMode(motorControl, OUTPUT);
  Serial.println("HX711 calibration sketch");
  Serial.println("Remove all weight from scale");
  Serial.println("After readings begin, place known weight on scale");
    scale.set_scale();
    scale.tare();  //Reset the scale to 0
   //Get a baseline reading
    long zero_factor = scale.read_average();
   
    //This can be used to remove the need to tare the scale.
    // Useful in permanent scale projects.
 
    Serial.print("Zero factor: ");
    Serial.println(zero_factor);
    // Reset pin (to rezero the weight not reset the board!)
    pinMode(RST, INPUT_PULLUP);
     delay(1000);
 
}

void loop() {
   scale.set_scale(calibration_factor);
  // Read an average of X readings
  float weight = scale.get_units(10);

  // An intermediate weigth value that we round according to some rules
  int netWeight = 0 ;

  // Make scale more sensitive at lower end
  // Weight > X then just round to nearest integer

  if (weight >= 5.0) {
    netWeight = (weight * 10.0);
    weight = (int)(0.5 + (netWeight / 10.0));
  }

  // Weight < Y then call it zero

  else if (weight > -0.01 && weight <= 0.01) {
   
    weight = 0;

  }

  // Any other weight will have 1 dec. place of precision

  else {

    netWeight = (weight * 10.0);
    weight = (netWeight / 10.0);

  }
  // Only print something out if it has changed (weight or cal. factor)

  if (calibration_factor != oldCal_factor || weight != oldWeight) {
    oldCal_factor = calibration_factor;
    oldWeight = weight;
    Serial.print("Reading: ");
    Serial.print(weight, 2);
    Serial.print(" g");
    Serial.print(" calibration_factor: ");
    Serial.print(calibration_factor);
    Serial.println();

  }

   petriDish=sr04.Distance();
   Serial.print(petriDish);
   Serial.println("cm");
   if ( petriDish <= 10){
      do {
      digitalWrite(motorControl, HIGH);
      Serial.println("motoring");
      }  while( weight <=30);
   }
   digitalWrite(motorControl, LOW);
   delay(100);
}
7
Maybe think about multiplexing the potentiometers and use a button and led to keep track of the function. You'll save yourself alot of wiring.
8
Displays / Re: 16x4 LCD stopped working
Last post by floresta - Today at 03:46 am
We can't tell if your code matches your wiring without seeing a photo of your setup.

How is the contrast set? 

Don
9
General Electronics / Re: ESP-12 Project (is the esp...
Last post by Wawa - Today at 03:44 am
for the ESP12, i have a 10uF (C3) cap there. are you suggesting adding an additional cap to the esp12 and the rest of the ICs or is the esp12 good?

i saw a couple of examples on the internet and most of them have a 10k there. i dont know its purpose
Wise to have 100n local decoupling very close to VDD/GND of every chip.

The ESP draws about 80mA, and ~400mA peaks during transmit.
You can almost eliminate those peaks with a 220-1000uF buffer cap on the supply pin of the ESP.
If you don't, then the supply and the 3.3volt regulator has to provide those peak currents.
Up to you...

Sometimes a 10k pull up resistor is used from the address pin to VDD, and a solder bridge to ground.
So you can change the I2C address with a blob of solder if you need to use more than one ADS chip.


The divider and the cap from tap to ground is ok.

But the CT must be connected directly to the inputs.

You need four 100k resistors.
One from the tap to each of the four inputs.
Leo..
10
Programming Questions / Re: LCD menu with encoder
Last post by odometer - Today at 03:44 am
It's no trouble.  It's just nice when debugging.  You think it locked up because it quit outputting to serial or doing whatever.  Blinky light lets you know if the program is still turning over or not. 
I don't understand. If the program didn't lock up, then why would it have quit "outputting to Serial or doing whatever"?
Pages: [1] 2 3 ... 10