First, I wrote 2 programs independently to move my servo and to get the distance the LIDAR is measuring.
Now I try to put these programs together but when I execute the code below, the servo doesn't work like it does when I run it alone : it shakes, and doesn't initalize correctly.
What can I do ?
Thanks for your help
#include <Servo.h>
#include <SoftwareSerial.h>
Servo myservo;
SoftwareSerial Serial1(3,2);
const int servoAnalogOut=A1;
unsigned int servoValue0Deg, servoValue180Deg;
int pos = 0;
int dist;
int strength;
float temprature;
int check;
int i;
int uart[9];
const int HEADER=0x59;
void setup() {
myservo.attach(9);
Serial.begin(115200);
Serial1.begin(115200);
}
void loop() {
for (int i=0; i <= 180; i++)
{
myservo.write(i);
delay(100);
Serial.println(map(analogRead(servoAnalogOut),servoValue0Deg,servoValue180Deg, 0, 180));
if (Serial1.available())
{ //check if serial port has data input
if(Serial1.read() == HEADER)
{
uart[0]=HEADER;
if (Serial1.read() == HEADER)
{
uart[1] = HEADER;
for (i = 2; i < 9; i++)
{
uart[i] = Serial1.read();
}
check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7];
if (uart[8] == (check & 0xff))
{
dist = uart[2] + uart[3] * 256;
strength = uart[4] + uart[5] * 256;
temprature = uart[6] + uart[7] *256;
temprature = temprature/8 - 256;
Serial.print("dist = ");
Serial.print(dist);
Serial.print('\t');
Serial.print("strength = ");
Serial.print(strength);
Serial.print("\t Chip Temprature = ");
Serial.print(temprature);
Serial.println(" celcius degree");
}
}
}
}
void calibration(){
myservo.write(0);
delay(2000);
servoValue0Deg= analogRead(servoAnalogOut);
Serial.println("Pot value for 0 deg is " + String(servoValue0Deg));
delay(500);
myservo.write(180);
delay(2000);
servoValue180Deg= analogRead(servoAnalogOut);
Serial.println("Pot value for 180 deg is " + String(servoValue0Deg));
delay(500);
Serial.println("Now going to 90 Degrees");
myservo.write(90);
delay(2000);
]
}
}
}
Thanks for your answer, I now use the servotimer2 library so I had to rebuild my initialization functions and a few things: it's now operating but alone only...
My problem is that in the final code below, the sensor parameters aren't displayed so I assume one of the if(...) isn't verified and I think this is because of the servo.
For the servo I also use as you can see an analog in to measure the potenial : any chance this conflicts with the sensor ?
The other problem is that the servo is moving to the position I ask him to go but when he reaches this position he shakes by like -3 to +3 degres.
Do you have a solution ?
#include <ServoTimer2.h>
#include <SoftwareSerial.h>
ServoTimer2 servo1;
SoftwareSerial Serial1(3,2);
const int servoAnalogOut=A5;
unsigned int servoValue0Deg, servoValue160Deg;
int pos=550;
int dist;
int strength;
float temprature;
int check;
int i;
int uart[9];
const int HEADER=0x59;
void setup()
{
servo1.attach(9);
Serial.begin(115200);
Serial1.begin(115200);
calibration();
}
void loop()
{
delay(1000);
servo1.write(pos);
delay(1000);
if (Serial1.available()) {
if(Serial1.read() == HEADER) {
uart[0]=HEADER;
if (Serial1.read() == HEADER) {
uart[1] = HEADER;
for (i = 2; i < 9; i++)
{ //save data in array
uart[i] = Serial1.read();
}
check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7];
if (uart[8] == (check & 0xff)) {
dist = uart[2] + uart[3] * 256;
strength = uart[4] + uart[5] * 256;
temprature = uart[6] + uart[7] *256;
temprature = temprature/8 - 256;
Serial.print("dist = ");
Serial.print(dist);
Serial.print('\t');
Serial.print("strength = ");
Serial.print(strength);
Serial.print("\t Chip Temprature = ");
Serial.print(temprature);
Serial.println(" celcius degree");
}
}
}
}
delay(1000);
Serial.println(map(analogRead(servoAnalogOut),servoValue0Deg,servoValue160Deg, 0, 160));
pos+=13;
}
void calibration(){
servo1.write(550);
delay(2000);
servoValue0Deg= analogRead(servoAnalogOut);
Serial.println("Pot value for 0 deg is " + String(servoValue0Deg));
delay(500);
servo1.write(2500);
delay(2000);
servoValue160Deg= analogRead(servoAnalogOut);
Serial.println("Pot value for 180 deg is " + String(servoValue160Deg));
delay(500);
}
Do you have a DMM?
If so, measure the voltage supply at the servo plug, it sounds like you need more current for the servo.
It is best if you can post your schematics into your post, export as jpg.
So I plugged a 5V 3amps alim on the arduino and still the same problem: the sensor doesn't enter the 3rd if(...) of the loop and the servo is shaking even with 0 instruction of movement given.
Your external supply is connected to the power plug on the Uno. That feeds the weak onboard 5V regulator. The 5V regulator cannot supply sufficient current for the servo so the servo twitches. The servo's motor pulls stall current every time that it starts and it starts a lot while trying to maintain position. Connect the servo to that power supply as shown in post #11 so the the external supply is powering the servo, not the puny on board regulator.
Is there a way to connect an external supply to that shield so that the shield supplies the servos? Can you post a data sheet, schematic or technical documentation on the sheild?
If the USB lead was still plugged in, then the power would still have come from USB.
The DC socket needs at least 7volt for the Uno to change from USB to DC socket supply.
That V1.2 shield has no option for external power.
According to the Eagle files I found, all white sockets are powered from the Uo's 5volt rail.
Leo..