New to Arduino - and can't get program to work

Hi

I am trying to do a model of a car with wings I have 2 'push to make switches' which are connected to ground and pin 2 & 3. when they are pushed independently I want the wings to go out and in.

this is my program any help very much appreciated!

Servo servo_R; // create servo object for right wing

#define out 2 // pin 2 is connected to out button
#define in 3 // pin 3 is connected to in button

void setup() {

servo_L.attach(10); // attaches the left servo on pin 8 to the servo object
servo_R.attach(12); // attaches the right servo on pin 10 to the servo object
pinMode(2,INPUT_PULLUP); // pin 2 recive signal for wing to go out to 90 degrees
pinMode(3,INPUT_PULLUP); // pin 3 recive signal for wing to go back to 0 degrees
servo_L.write(0);
servo_R.write (75);
}

void loop() {
if(digitalRead(out) == LOW)
servo_L.write (60); // move the left servo to 90 degrees
servo_R.write (15); // move the right servo to 90 degrees

if(digitalRead(in) == LOW)
servo_L.write (0); // move the servo to 0 degrees
servo_R.write (75); // move the servo to 0 degrees
}

Hi, welcome to the forum! Please have a look at How to use the forum. In particular the parts about:

  • using code tags
  • describing the problem. "Does not work" isn't an answer. What do you want to do? What really happens?

Three remarks about the problem already:

  • Why define names if you never use them?
  • What happened to the creation of servo_L?
  • Servo's can't be powered by the Arduino, they need a separated power supply.

O.k. that code isn't complete and won't compile because you've left a few lines out (at least).

And if you're going to have comments hanging around make them mean something. write(15) is never going to move a servo to 90 degrees and attach(8) is not going to do anything with pin 10.

So what is supposed to happen? And what actually does happen?

Steve

Hi Steve

Thanks for the reply

That was the original code I had before I found the position (Degrees) of the out and in or home position. What I would like to happen is when 2 & 3 receive a ground signal that the wings move to the position for out and in.

The latest code I am using is below with the comments changed

The servos have a power supply separate to the Arduino.

I have got the wings to move in and out by uploading single line commands that is how I found the degrees I need for the in and out position for each servo.

Pin 2&3 are connected to 2 'push to make' buttons which are connected to ground.

I want one button when pressed to move the wings to the out position and the other to move them back in to the home position.

This is my first ever program with Arduino and I have been through hours of Youtube videos trying to get this code working.

#include <Servo.h>

Servo servo_L; // create servo object for left wing
Servo servo_R; // create servo object for right wing

#define out 2 // pin 2 is connected to out button
#define in 3 // pin 3 is connected to in button

void setup() {

servo_L.attach(10); // attaches the left servo on pin 10 to the servo object
servo_R.attach(12); // attaches the right servo on pin 12 to the servo object
pinMode(2,INPUT_PULLUP); // pin 2 recive signal for wing to go to out position
pinMode(3,INPUT_PULLUP); // pin 3 recive signal for wing to go to home position
servo_L.write(0); //this is the home position for left wing
servo_R.write (75); // this is the home position for right wing
}

void loop() {
if(digitalRead(out) == LOW)
servo_L.write (60); // move the left servo to out position
servo_R.write (15); // move the right servo to out position

if(digitalRead(in) == LOW)
servo_L.write (0); // move the servo to home position
servo_R.write (75); // move the servo to home position
}

A small reminder to reply #1 ::slight_smile:

You still haven't told us what your code does. But I see at least two problems. When you want multiple lines of code to be controlled by an if statement or similar they MUST be enclosed by braces {}. E.g.

  if(digitalRead(out) == LOW)
  {
    servo_L.write (60); // move the left servo to out position
    servo_R.write (15); // move the right servo to out position
   }
// without the {} the if statement only controls the first line, the second line executes unconditionally

And you've missed them out for the other if statement too.

Steve