I need help with code!

Hello!

I am currently working on a project, but i am really struggling with the code part. I just started getting into code and i would really appreciate it if someone can help.

When you press on the first button, the servo motor turns 90 degrees clockwise, 10 second delay, then 90 degrees anti-clockwise

When you press on the second button, the same same servo motors turns 90 degrees anticlockwise, 10 second delay, then 90 degrees clockwise

I'd really appreaciate the help

Thank you!

"Help" round here means you make an attempt at producing a program then when you have posted it here we help you to get it right.

There are examples in the IDE about reading buttons and others about using servos. Have a look at them and see what you can do.

Steve

What usually happens here is that you tell us what equipment you have, how you’ve connected it up, and what lessons/tutorials you’ve completed.

By then you’ll have at least the simplest sketch working (because you followed the lessons & tutorials).

Try to code your own, post your attempt - then we help you get it sorted.

That should keep you busy for a day or two.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Have you looked in the Examples part of the IDE under Servo?

Tom... :slight_smile:

I’m sorry if i offended you guys in any way. I actually did work on the code, I am using 2 buttons and 1 servomotor, and here it is (I made 2):

First:
int button1 = 1;
int button2 = 2;
Servo myservo;

int pos = 0; // variable to store the servo position

void setup() {
pinMode(button1, INPUT);
pinMode(button2, INPUT);
myservo.attach(13);
}

void loop() {
digitalRead(button1);
for (pos = 0; pos <= 90; pos += 1) {

myservo.write(pos);
delay(15000);
}
for (pos = 90; pos >= 180; pos -= 1) {
myservo.write(pos);
delay(15000);
}
{ digitalRead(button2)
for (pos = 0; pos <= 90; pos += 1) {

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

Second:
// pushbutton pin
const int buttonPin = 0;

// servo pin
const int servoPin = 1;
Servo servo;

//create a variable to store a counter and set it to 0
int counter = 0;
void setup()
{
servo.attach (servoPin);

// Set up the pushbutton pins to be an input:
pinMode(buttonPin, INPUT);
}

void loop()
{
// local variable to hold the pushbutton states
int buttonState;

//read the digital state of buttonPin with digitalRead() function and store the //value in buttonState variable
buttonState = digitalRead(buttonPin);

//if the button is pressed increment counter and wait a tiny bit to give us some //time to release the button
if (buttonState == LOW) // light the LED
{
counter++;
delay(150);
}

if(counter == 0)
servo.write (20); // zero degrees
else if(counter == 1)
servo.write(90);
else if(counter == 2)
servo.write (150);
else if(counter == 3)
servo.write (180);
//else reset the counter to 0 which resets thr servo to 0 degrees
else
counter = 0;
}

So?

What are suppose to do with that code?

.

Hi,

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Do not make pin0 and pin1 digital, they are the programming pins.

Use pin2 and pin7 as an example

Thanks.. Tom... :slight_smile:

I see two instance of [color=blue]void loop()[/color]. There can be only one for proper operation.

dougp:
I see two instance of [color=blue]void loop()[/color]. There can be only one for proper operation compilation.

The OP did say he was posting two attempts “I made 2”…so it’s not surprising there are two loop()s. There are also two setup()s.

So we have two separate programs. Do either of them compile and load and do anything useful? If so choose the one that is nearest to doing what you want, test it, post just that one correctly using </> code tags. Then tell us exactly what it does and what you want it to do that it doesn’t yet do.

Then we can maybe offer some real help.

Steve

Use code tags.

The delay() function is very common in the simple tutorials. However it is rarely used in real programs. Find the Blink-Without-Delay tutorial and modify that to do what you want.

Use code tags, so your code looks like this, and we can actually see the two different codes in separate blocks:

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

This part will be running very slowly (moving 1 degree per 15 seconds), it's going to take an awfully long time for this loop to finish - about 270 hours - and in the meantime the servo will start doing all kinds of craziness due to undocumented behaviour of the servo.write() function for position values >200.

How are the buttons wired?

wvmarle:
Use code tags, so your code looks like this, and we can actually see the two different codes in separate blocks:

  for (pos = 90; pos >= 180; pos -= 1) {

myservo.write(pos);           
    delay(15000);                     
  }



This part will be running very slowly (moving 1 degree per 15 seconds), it's going to take an awfully long time for this loop to finish - about 270 hours -

I see a very short loop. Or lack of loop.

in the meantime the servo will start doing all kinds of craziness due to undocumented behaviour of the servo.write() function for position values >200.

Nope. No craziness until you reach 544. It’s even written in an accurate comment. Not that I ever trust comments.

void Servo::write(int value)
{  
  if(value < MIN_PULSE_WIDTH)
  {  // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
    if(value < 0) value = 0;
    if(value > 180) value = 180;
    value = map(value, 0, 180, SERVO_MIN(),  SERVO_MAX());      
  }
  this->writeMicroseconds(value);
}