My circuit not Working.

Hi, im doing c code for my project and after done verify the done, i try to upload it to my arduino uno board. But it didn’t work, My circuit have 3 input and 3 output. 3 Push button and another 3 is LED,Buzzer and Servo Motor. I try to fix the problem but i didn’t manage to find it. I attached the code below. Please help me.

servo_motor.ino (729 Bytes)

Hi, im doing c code for my project and after done verify the done, i try to upload it to my arduino uno board. But it didn’t work, My circuit have 3 input and 3 output. 3 Push button and another 3 is LED,Buzzer and Servo Motor. I try to fix the problem but i didn’t manage to find it. I attached the code below. Please help me.

servo_motor.ino (729 Bytes)

Farid23: Hi, im doing c code for my project and after done verify the done, i try to upload it to my arduino uno board. But it didn't work, My circuit have 3 input and 3 output. 3 Push button and another 3 is LED,Buzzer and Servo Motor. I try to fix the problem but i didn't manage to find it. I attached the code below. Please help me.

You need to describe what it was suppose to due and then what it actually did.

When you define the value for some of the pins,

int pos = 0;
int LED = 12;   // do you actually mean 13? The reason I ask, is that The Arduino has a LED connected to pin 13;
int BUZZER = 8;
int BUTTON1 = 4;
int BUTTON2 = 7;

You might want to actually use these declared values for something. Like this:

void setup ()
{
  myservo.attach(2);  
  pinMode(LED,OUTPUT);
  pinMode(BUZZER,OUTPUT);
  pinMode(BUTTON2,INPUT);
  pinMode(BUTTON1,INPUT);
}

Instead of hardcoding the pin numbers. It is easier to understand:

pinMode(LED,OUTPUT);
//than
pinMode(13,OUTPUT);

Chuck.

Farid23: i try to upload it to my arduino uno board. But it didn't work,

Is it throwing any error? If not what is your expected result? Explain the requirement. Also use Code tag to embed the program.

OP’d code for reference.

#include <Servo.h>

Servo myservo;

int pos = 0;
int LED = 12;
int BUZZER = 8;
int BUTTON1 = 4;
int BUTTON2 = 7;


void setup ()
{
  myservo.attach(2);
  pinMode(12, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, INPUT);
  pinMode(4, INPUT);
}

void loop()
{
  if (digitalRead(2) == HIGH)
    for (pos = 0; pos <= 180; pos += 1)
    {
      myservo.write(pos);
      delay(15);
    }

  for (pos = 180; pos >= 0; pos -= 1)
  {
    myservo.write(pos);
    delay(15);
  }

  if (digitalRead(4) == HIGH)
  {
    digitalWrite(12, HIGH);
  }
  if (digitalRead(7) == HIGH)
  {
    digitalWrite(8, HIGH);
  }

  else {
    digitalWrite(12, LOW);
    digitalWrite(8, LOW);
  }
}

(deleted)

Why give the pins nice friendly names if you’re not going to use them?

You attached your servo to pin 2, then you do this:-if (digitalRead(2) == HIGH)Is that really what you intended?

And in this, you forgot a pair of braces for the ‘if’ statement:-

if (digitalRead(2) == HIGH)
        for (pos = 0; pos <= 180; pos += 1)
        {
            myservo.write(pos);
            delay(15);
        }

If you had hit Ctrl-T or >Tools >Auto format, this would have been immediately apparent.

Edit: Oh, and in future, please post your code, (in [code]code tags[/code]), rather than attaching it.

Maybe in your code:

void loop()
{
if(digitalRead(2) == HIGH)
for (pos = 0; pos <= 180; pos += 1)
{
myservo.write(pos);
delay(15);
}

for (pos = 180; pos >= 0; pos -= 1)
** {**
** myservo.write(pos); **
** delay(15); **
}

==> Maybe this part is being ‘lagged’ because since it’s not inside the ‘if’ function, it would do that every loop. So if you pushed the button (pin 2), then it would just go back and forth 180 degrees. But if you don’t give a signal to the button, then I assume it wouldn’t have enough time to go to 180 degree quickly… So that ‘might’ be a problem

///////////////////////////////
if(digitalRead(4) == HIGH)
{
digitalWrite(12,HIGH);
}
if (digitalRead(7) == HIGH)
{
digitalWrite(8,HIGH);
}

else{
digitalWrite(12,LOW);
digitalWrite(8,LOW);
}
}

When you do add those braces, I assume that you want them around both servo movements, as suggested by junwoo0914. (I accidentally left out the second movement in the code snippet I quoted.)

You really want this, but you’ll need to read a different pin for the conditional:-
(Note the formatting.)

if (digitalRead(2) == HIGH)  // *** Not pin 2, it's the servo!
{
    for (pos = 0; pos <= 180; pos += 1)
    {
        myservo.write(pos);
        delay(15);
    }

    for (pos = 180; pos >= 0; pos -= 1)
    {
        myservo.write(pos);
        delay(15);
    }
}

An extra tip. To increment and decrement ‘pos’, it’s quicker and easier to do this:-pos++orpos--rather thanpos+=1orpos-=1

I see that you've also started another thread on this:- My circuit didn't work as it should be, maybe my code have some problem. I wish I hadn't wasted my time with you now. :(

Servo attached on pin 2. Trying to read pin 2 HIGH in order to write on pin 2 servo. Lot wrong there.

@Farid23, do not cross-post. Threads merged.