Servo motor reacts to other things - arduino

I am working with Arduino, I connected a servo motor and a normal motor. They both work but when I start the normal motor script, the servo motor does small spastic stuff.
Could anyone help me with this?
// Includes
#include <Servo.h>
// Aanmaken van de variabelen voor in de code
int ledPin = 13;
const int motorPin = 2;
int usbnumber = 0;
Servo stuurServo; // create servo object to control a servo
int pos = 90; // variable to store the servo position
// De eerste setup maken
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(motorPin, OUTPUT);
stuurServo.attach(12);
Serial.begin(9600);
stuurServo.write(pos);
}
void loop()
{
if (Serial.available() > 0) {
usbnumber = Serial.read();
}
if (usbnumber > 0) {
if (usbnumber == 1){ // Lampje knipperen
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(500);
}else if(usbnumber == 2){ // Motor aan voor 5 seconden
digitalWrite(motorPin, HIGH);
delay(20000);
digitalWrite(motorPin, LOW);
}else if(usbnumber == 3){ // stuur servo +10 graden
if(pos != 180){
pos + 10;
stuurServo.write(pos);
}
}else if(usbnumber == 4){ // stuur servo -10 graden
if(pos != 0){
pos - 10;
stuurServo.write(pos);
}
}else if(usbnumber == 5){ // stuur servo liks
pos = 0;
stuurServo.write(pos);
}else if(usbnumber == 6){ // stuur servo rechts
pos = 180;
stuurServo.write(pos);
}else{
delay(500);
}
usbnumber = 0;
}
}

Most (hobby) servo motors will twitch or give a little jerk when they are powered up, especially if you power the motor before driving the servo (providing a position control signal). The solution is to write to the control line to the servo before allowing power to it. Some easy solutions include:
controlling power to the servo through something you can turn off/on (PWM, MOSFET someone else help here?)
having a secondary power manual switch you can toggle once your controller is up and running.
There is basically nothing you can do in code without some way to have the circuit start up with no power to the Servo until you are driving the position control line.

Related

how to set servo to rotate only once in 1 condition

I want to make a gate using 360 servo and ultrasonic sensor, here I use if else. when ultrasonic conditions >= 10 cm the servo should rotate one time to the right for 5 seconds, and vice versa. but when it reaches the condition >= 10 cm where the servo continues to rotate without stopping, how to make it stop in 1 rotation? and also i need distance data from ultrasonic sensor to display.
I'm a beginner in this, I will be very grateful for the help.
this is my code :
#include <Wire.h>
#include <Servo.h> //Servo library
#define echoPin 3 //triger pin, echo pin
#define triggerPin 4
int waktu;
int jarak;
int hasiljarak;
Servo servo;
void setup()
{
Serial.begin(9600);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
waktu = pulseIn(echoPin, HIGH);
jarak = waktu * 0.034 / 2;
Serial.print(jarak);
Serial.print(" CM");
delay(100);
servo.attach(9);
if(jarak<=10)
{
servo.write(2000);
delay(5000);
}
else if(jarak>=30)
{
servo.write(1000);
delay(5000);
}
else
{
servo.write(1500);
delay(1000);
}
servo.detach();
}
i've finish the servo with this code
void loop()
{
int ultra1 = tinggiUltra - sonar.ping_cm();
int ultra2 = tinggiUltra - sonar2.ping_cm();
delay(1000); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print(ultra1); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
Serial.print(ultra2);
Serial.println("cm");
if (ultra1 >= 11 && ultra1 <= 40 && ultra2 >=11)
{
servo.write(posClose);
delay(2000);
}
else if (ultra1 >= 5 && ultra1 <= 10 && ultra2 <=10)
{
servo.write(posOpen);
delay(2000);
}
else if (ultra2 <= 4 && ultra2 >= 0)
{
servo.write(pos);
delay(2000);
}
statPin = servo.read();
}
delay(2000);
}
else if (ultra1 >= 5 && ultra1 <= 10 && ultra2 <=10)
{
servo.write(posOpen);
delay(2000);
}
else if (ultra2 <= 4 && ultra2 >= 0)
{
servo.write(pos);
delay(2000);
}
statPin = servo.read();
}
for the servo I use it works normally as i wanted, and I use the MG955 360 servo. for the project I made using threads to open and close the door. I used if else for the code, and it worked fine by adding a parameter to restrict the movement of the door that I made. and thanks for the answer
if someone have to correct my program code, i'm grateful

Arduino Nano and analog joystick

I have a game that consist of 4 direction of movement (up down left and right)
using Arduino Nano and analog joystick, seems like code is right as check before other posts.
This is the Arduino code:
byte x_axis = A3;
byte y_axis = A1;
byte btn1 = 8;
byte btn2 = 9;
byte btn3 = 10;
byte btn4 = 11;
byte btn5 = 12;
byte led = 13;
void setup(){
pinMode(x_axis, INPUT);
pinMode(y_axis, INPUT);
pinMode(btn1, INPUT);
pinMode(btn2, INPUT);
pinMode(btn3, INPUT);
pinMode(btn4, INPUT);
pinMode(btn5, INPUT);
digitalWrite(btn1, HIGH);
digitalWrite(btn2, HIGH);
digitalWrite(btn3, HIGH);
digitalWrite(btn4, HIGH);
digitalWrite(btn5, HIGH);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
Serial.begin(9600);
}
void loop(){
Int read_x = analogRead(x_axis);
int read_y = analogRead(y_axis);
if(read_x > 600){
Serial.println("R");
digitalWrite(led, HIGH);
}
if(read_x < 400){
Serial.println("L");
digitalWrite(led, HIGH);
}
if(read_y > 600){
Serial.println("D");
digitalWrite(led, HIGH);
}
if(read_y < 400){
Serial.println("U");
digitalWrite(led, HIGH);
}
if(digitalRead(btn1) == LOW){
Serial.println("1");
digitalWrite(led, HIGH);
}
if(digitalRead(btn2) == LOW){
Serial.println("2");
digitalWrite(led, HIGH);
}
if(digitalRead(btn3) == LOW){
Serial.println("3");
digitalWrite(led, HIGH);
}
if(digitalRead(btn4) == LOW){
Serial.println("4");
digitalWrite(led, HIGH);
}
if(digitalRead(btn5) == LOW){
Serial.println("5");
digitalWrite(led, HIGH);
}
delay(10);
digitalWrite(led, LOW);
}
But when I use serial monitor to check it, it non stop show me U and L even without touching the joystick.
How can I fix this problem?
I'm feeling generous, so you have some jitter issues in the code and you really could use some cleanup. This code compiles. Ok now when a joystick is 0,0 in x/y physically it jitters in code. One thing you could do is remap out the jitter to give a wider center. print the raw analog values to the serial monitor and then map them out to your 0 point with a little padding. reference: https://www.arduino.cc/reference/en/language/functions/math/map/
byte x_axis = A3;
byte y_axis = A1;
byte btn[] = {8, 9, 10, 11, 12}; // 2,3,4,5,6
byte stat[] = {1, 2, 3, 4, 5};
byte led = 13;
int dval = 50;
void setup() {
pinMode(x_axis, INPUT);
pinMode(y_axis, INPUT);
for (int i = 0; i < 5; i++) {
pinMode(btn[i], INPUT);
digitalWrite(btn[i], HIGH);
}
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
Serial.begin(9600);
}
void loop() {
int read_x = analogRead(x_axis);
delay(dval);
int read_y = analogRead(y_axis);
delay(dval);
if (read_x > 600) {
Serial.println("R");
digitalWrite(led, HIGH);
}
if (read_x < 400) {
Serial.println("L");
digitalWrite(led, HIGH);
}
if (read_y > 600) {
Serial.println("D");
digitalWrite(led, HIGH);
}
if (read_y < 400) {
Serial.println("U");
digitalWrite(led, HIGH);
}
for (int i = 0; i < 5; i++) {
if (digitalRead(btn[i]) == LOW) {
Serial.println(stat[i]);
digitalWrite(led, HIGH);
}
}
delay(10);
digitalWrite(led, LOW);
}
Emad joon:
1- Check that the ground of your joy stick and the Vdd is connected to your arduino.
2- connect the x and y of the joy stick to the arduino analog inputs.
use this code as starter:
#define X_AXIS A1
#define Y_AXIS A3
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("X = ");
Serial.println(analogRead(X_AXIS);
Serial.print("Y = ");
Serial.println(analogRead(Y_AXIS);
delay(150);
}
See what values you get when your Joy Stick is at initial position. You can build up on this code for buttons.
Also, do not forget to use debouncing for your button readings:
if(digitalRead(Button1)==0){
delay(40);
if(digitalRead(Button1==0){
buttonPressed=true;
}
}

How to write a state change (edge detection) using analogRead() on Arduino?

I'm having trouble creating a toggle switch using an analog input connected to an RF receiver. I'm attempting to make a servo turn 180 degrees when the voltage reads higher than 800 and stay at 180 degrees until the button is pressed again. It only reads higher than 800 when I press a button on my key fob (RF transmitter). In my code it does this, but it doesn't hold at 180 degrees and wait until the button is pressed again to go back to 0 degrees.
#include <Servo.h>
Servo myservo;
const int analogInPin = A0;
int led = 13;
float sensorValue = 0;
void setup(){
Serial.begin(2400);
pinMode(led, OUTPUT);
myservo.attach(6);
myservo.write(0);
}
void loop(){
sensorValue = analogRead(analogInPin);
Serial.print("Voltage Output = ");
Serial.print(sensorValue);
Serial.println(" ");
delay(100);
if (sensorValue > 800) {
digitalWrite(led, HIGH);
myservo.write(180);
delay(100);
}
else{
digitalWrite(led, LOW);
myservo.write(0);
delay(100);
}
}
EDIT: I'm pretty close with this edited code. I added a variable and an if statement. It turns the LED on and it stays on, but when pressed again it doesn't turn off. So close...
const int analogInPin = A0;
int led = 13;
int ledState = 0;
float sensorValue = 0;
void setup(){
Serial.begin(2400);
pinMode(led, OUTPUT);
}
void loop(){
sensorValue = analogRead(analogInPin);
Serial.print("Voltage Output = ");
Serial.print(sensorValue);
Serial.println(" ");
delay(100);
if (sensorValue < 400 && ledState == 0) {
digitalWrite(led, HIGH);
delay(500);
ledState == 1;
}
if (sensorValue < 400 && ledState == 1) {
digitalWrite(led, LOW);
delay(500);
ledState == 0;
}
}
OK. I think I solved it! Take a look. It now turns the LED on when the button is pressed and stays on until the button is pressed again. This is how to create a switch using an analog input reading.
const int analogInPin = A0;
int led = 13;
int ledState = 0;
float sensorValue = 0;
void setup(){
Serial.begin(2400);
pinMode(led, OUTPUT);
}
void loop(){
sensorValue = analogRead(analogInPin);
Serial.print("Voltage Output = ");
Serial.print(sensorValue);
Serial.println(" ");
delay(100);
if (sensorValue < 400 && ledState == 0) {
digitalWrite(led, HIGH);
delay(100);
ledState = 1;
Serial.println(ledState);
delay(500);
}
else {
if (sensorValue < 400 && ledState == 1) {
digitalWrite(led, LOW);
delay(100);
ledState = 0;
Serial.println(ledState);
delay(500);
}
}
}

How do i program this arduino code

arduino code for programming a analoge pot to control 2 leds,1 to come on at 1.5V and the other other at 3.5v first time using this any help will do thanks
This should work:
#define LED1 4
#define LED2 5
#define POT A0
void setup()
{
pinMode(POT, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
}
int val = 0;
void loop()
{
val = analagRead(POT);
if(val > 308) // 1.5/max voltage (say 5) x resolution (1024)
digitalWrite(LED1, HIGH);
else if(val > 717) // 3.5/max voltage (say 5) x resolution (1024)
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH) ;
}
else
{
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
}
}
Fix some syntax errors, I am typing from cellphone.

Arduino, if else LED "stuck"

So the code is not working properply, there are two leds that wont turn off "highliten" the problem. when i run the Else part of the program. i want to turn them off in the else part. :)
include
byte ledPin[] = {8, 9, 10, 11, 12, 13}; //--------------------------------.
int ledDelay; // Del 1
int direction = 1;
int currentLED = 0;
unsigned long changeTime;
int potPin = 0;
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int va;
void setup()
{
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, INPUT);
myservo.attach(3); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
for (int x=0; x<6; x++) {
pinMode(ledPin[x], OUTPUT); }
changeTime = millis();
}
void loop() {
int on = digitalRead(6);
if (on == HIGH)
{
myservo.attach(3);
// Here is the problem!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if va < 523)
{
digitalWrite(5, HIGH);
}
else if (va > 555)
{
digitalWrite(4, HIGH);
}
else
{
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}
// Here is the problem!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
va = analogRead(potPin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(va, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(1); // waits for the servo to get there
}
else
{
myservo.detach();
digitalWrite(5, LOW);
digitalWrite(4, LOW);
ledDelay = analogRead(potPin) / 4;
if ((millis() - changeTime) > ledDelay)
{
changeLED();
changeTime = millis();
}
}
}
void changeLED() {
for (int x=0; x<6; x++)
{
digitalWrite(ledPin[x], LOW);
}
digitalWrite(ledPin[currentLED], HIGH);
currentLED += direction;
if (currentLED == 6) {direction = -1;}
if (currentLED == 0) {direction = 1;}
}
in advance Thank you!
Right at the end of the sketch you have the following line:
if (currentLED == 6) { direction = -1; }
I think, without actually running the program, that the problem is here. In the previous line you have added one to the value of currentLED and you are checking to see if you have gone off the end of the ledPin array. You change the direction but you don't reset the currentLED position to be back inside the ledPin range.
The next time changeLED is called it tries to call digitalWrite(ledPin[currentLED], HIGH); but the value of currentLED is 6, which is outside the ledPin array. The Arduino probably gets upset at this point.
I think you simply need to change the statement to check whether currentLED == 5 rather than 6. This will mean that next time that changeLED is called the last LED will be turned on and the value of currentLED will be decremented (direction == -1), keeping it inside the ledPin range.

Resources