Show Posts
Pages: [1] 2
1  Using Arduino / Programming Questions / Re: 2 Servo's sweep opposite directions via push button. on: February 19, 2014, 12:35:31 pm
Code:
for(pos = 0; pos < 180; pos=pos+1)// dont think this needs to be here, but doesnt work without it
       {
        myservo.writeMicroseconds(1000);  // tell servo to go to position at supersonic speed, cant change speed
        myservo1.writeMicroseconds(2000);
Have you even been listening to what people have been telling you?
Didn't we cover the use of "pos" about thirty replies ago?
No wonder they're moving too fast.
(BTW milliseconds != microseconds)

Yep, was covered about 30 replies back, so took it out, then the code didn't work, so put it back. I think you forget how excruciatingly hard this is when you know literally NOTHING!!! If an explanation is written in total 100% english as if talking to a baby, then i understand it. Most explanations here have been given to me with the inclusion of other sh*t i don't understand. Its like teaching an English person how to say kiss my *ss in spanish, but explaining it to them in SPANISH. Even the things that seem mind numbingly simple to you look like this to me: kjuvdefyuvosadyubc oawyobds couadwvCYODSCVYOAEVDFOQWAV


Take this for example written by zoomkat:
"For further debugging, I added a line in the startup section of the code that prints out when starting the serial monitor. Note that when the serial monitor opens it should be indicating 9600 baud and no line ending in the lower right hand corner of the serial monitor. I tested the below code and when pin  is connected to ground, the toggling is printing out to the serial monitor. I suggest you try the below code with the servos disconnected from the arduino, eliminating possible servo issues. "

First i have to look up wtf debugging means. Then i have to work out which the startup part is, is that void setup? void loop? Or is it something completely different?To you thats simple, but its not when you don't know how this stuff works..

Now whats a serial monitor? I'll go look that up too... done... 9600 baud... still hav e no idea what that means but i've just spent 30 minutes looking up the first few other things.

Now how will i know toggle is printing to serial monitor? What can i expect it to say? Will i just know when i look at it? Turns out it was quite simple when i got there.

If i ask any simple questions, i get the piss taken out of me, despite the rules saying if you don't understand something then ask. You lot seem to be totally oblivious to how bloody hard this is. You must have been born with a circuit in your mouth.

So have i listened? Avoiding the obvious sarcasm of being textual not audible, yes i have listened, i've listened to post after post explaining things in this fashion: "Have you tried kbqwdiuvbqidv? Or why don't you add jubhfcuhwebul to the i;wabcpuwbc while ;iduwbfviubwfuv.....?"
2  Using Arduino / Programming Questions / Re: 2 Servo's sweep opposite directions via push button. on: February 19, 2014, 12:12:44 pm
Sorry late reply.
Zoomkat, in the serial monitor your code appeared to be doing as it should but was having no effect on the physical hardware. I have therefore given up before my mac gets launched through a window.... Windows and macs are not compatible so i have heard....

For some reason nobody elses code would work for me and i am running low on time now.

I have chosen to go with my original code with Robins advice from page 1 to use the write.microseconds command instead of the degrees. I really like that.
The Servos are moving to fast for me but I will have to make do, altering the delay just makes it take longer before the servos will respond to the button again. They still move supersonic speed. Also I'm having to use a resistor because I can't work out how to get rid of it.

I think the code still has unnecessary commands but i don't know how to take them out. So here is the code i went for.

My comments show my confusion. The hardware is being installed tonight so there is literally no further time for my "learning curve". If anybody wishes to tidy it up I'd be grateful. If not, thank you all again for your help...

Code:
#include <Servo.h>

 //Set digital pin numbers:
 const int servoPin = 10; // The pin number of Servo
 const int servoPin1 = 9; // The pin number of Servo 1
 const int buttonPin = 2;  // The pin number of the Pushbutton
 int buttonState = 0;  // Variable for reading the pushbutton status
 int directionState = 0;  // Variable for reading direction of the servo

 Servo myservo;  // Create servo object to control a servo
 Servo myservo1; // Create a servo object to control a second servo
 int pos = 0;  // Variable to store the servo position

 void setup() {
 myservo.attach(10); // attaches servo on pin 10
 myservo1.attach(9); // attaches second servo on pin 9  
 pinMode(buttonPin, INPUT);  // initialize the pushbutton pin as an input
  
 }

 void loop()
 {
   myservo.write(0); // stores servo start position
   myservo1.write(180); // stores servo start position
   buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:

   if (directionState == 0){//The button is pushed
     if (buttonState == HIGH)
     {
       directionState = 1;// The direction for the servo is clockwise
       for(pos = 0; pos < 180; pos=pos+1)// dont think this needs to be here, but doesnt work without it
       {
        myservo.writeMicroseconds(1000);  // tell servo to go to position at supersonic speed, cant change speed
        myservo1.writeMicroseconds(2000);  // tell servo to go to position at supersonic speed, cant change speed
        delay(10);  // waits 10ms for the servo to reach the position
        
       }
     }

   } else if (directionState == 1) { // The button is pushed
     if (buttonState == HIGH)
     {
       directionState = 0;  // The direction for the servo is anti-clockwise
       for(pos = 180; pos>=0; pos=pos-1) // have absolutely no idea if this should be here, but it wont work without it
       {
        myservo.writeMicroseconds(2000);  // tell servo to go to position at supersonic speed, cant change speed
        myservo1.writeMicroseconds(1000);  // tell servo to go to position at supersonic speed, cant change speed
        delay(10);  // waits 10ms for the servo to reach the position
       }
     }
   }
 }
3  Using Arduino / Programming Questions / Re: 2 Servo's sweep opposite directions via push button. on: February 14, 2014, 07:40:31 pm
Serial monitor, blank white screen. Button pressed... still blank white screen. Button pressed lots of times... nothing but blank white screen.
4  Using Arduino / Programming Questions / Re: 2 Servo's sweep opposite directions via push button. on: February 14, 2014, 07:00:55 pm
Below is some basic servo/button toggle code. You might use this as a starting point to add the slow sweep function and servo position limits. I have some other basic code that I can post if needed. If I post code I usually try to post code that compiles and runs instead of sometimes confusing code "snippets".  smiley

Code:
//zoomkat servo button toggle test 4-28-2012

#include <Servo.h>
int button = 5; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
Servo servo1;
boolean toggle = true;

void setup()
{
  pinMode(button, INPUT); //arduino monitor pin state
  servo.attach(7); //pin for servo control signal
  servo1.attach(8); //pin for servo control signal
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
     
      servo.write(160);
      servo1.write(20);
      toggle = !toggle;
    }
    else
    {
      servo.write(20);
      servo1.write(160);
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}


Hmph, why do your codes never work for me? I've attached the servos to 7 and 8. I've attached the switch from 5 - gnd. I don't understand.
5  Using Arduino / Programming Questions / Re: 2 Servo's sweep opposite directions via push button. on: February 14, 2014, 05:37:18 pm
Robin, I've never seen such a thing written like that before but yes, thats what I want. Please note i have added a step 11 into my description. Its an important thing i feel i missed out.

UKhelibob... dare i ask, but should I be inputting that code EXACTLY as you have written it or am i supposed to be modding it? I'm guessing the latter but I just don't know where i stand with you in our relationship anymore... You're sending me mixed signals, I feel like your pushing me away smiley-wink


lol
6  Using Arduino / Programming Questions / Re: 2 Servo's sweep opposite directions via push button. on: February 14, 2014, 01:57:50 pm
In steps 1 and 6 is it the release that triggers the action, or the press ?  Sorry to sound picky but it will matter for the programming.

Um, it doesn't matter for this particular use i suppose. I'd be happy with either. whichever is easiest
7  Using Arduino / Programming Questions / Re: 2 Servo's sweep opposite directions via push button. on: February 14, 2014, 01:25:14 pm
Quote
To recap, i wish to press a button and have 2 servos move in opposite directions for 1 sweep. Then press the button and the servos sweep back to the start position.

May be time to clearly state in detail just what you want the servos to do. Questions:

1. Do you only want to use a single button to start the servo movement (requires a toggle function), or use two buttons, one for each desired servo movement direction?

2. Do you want the servos to make a full range movement on a button press, or do you want the servos to stop moving where they are when the button is released?

3. Do you want the servo movement to be slower than their normal full speed movement?





Okie pokes. I realise computer programming requires very very very specific detail. Here goes:

1. User is to Press THEN release a momentary push button. Press or release may trigger the rest of the code, it doesn't matter. (Button will later be replaced with a reed switch).
2. Servo 1 moves from 180 > 0 at a speed that I am able to change with relative ease if required during prototyping
3. Simultaneously Servo 2 moves from 0 > 180 at a speed that I am able to change with relative ease if required during prototyping
4. It is important that steps 2 and 3 occur at the exact same time, at the exact same speed, exact same distance
5. Upon reaching 180 and 0 respectively, they stop moving and do nothing. I wish for minimal power to be consumed during this time

6. User is to press THEN release a momentary push button. Press or release may trigger the rest of the code, it doesn't matter.(Button will later be replaced with a reed switch).
7. Servo 1 moves from 0 > 180 at a speed that i am able to change with relative ease if required during prototyping
8. Simultaneously Servo 2 moves from 180 > 0 at a speed that I am able to change with relative ease if required during prototyping
9. It is important that steps 7 and 8 occur at the exact same time, at the exact same speed, exact same distance
10. Upon reaching 0 and 180 respectively, they stop moving and do nothing. I wish for minimal power to be consumed during this time.

11. Repeat from step 1 permanently.

Requirement: They will both be utilised to lift a hatch. One Servo on the left side of the hatch, one servo on the right side of the hatch. They will be working together to share the weight. When a momentary button is pressed and released (later a reed switch contacts magnet concealed in hand), the servos must turn together to lift the hatch.Once hatch is in the up position it must remain here until i am ready to close it again. Once i have finished with it in the up position i will then press and release a momentary button (later a reed switch contacts a magnet concealed in hand), the servos must turn together to close the hatch. Once the hatch is in the down position it must remain here until I am ready to open it again.

Thats about as clear as i can get. i hope this is more useful
8  Using Arduino / Programming Questions / Re: 2 Servo's sweep opposite directions via push button. on: February 13, 2014, 05:36:02 pm
Momentary which will eventually be replaced with a reed switch.
9  Using Arduino / Programming Questions / Re: 2 Servo's sweep opposite directions via push button. on: February 13, 2014, 05:28:25 pm
Magic, my mistake. They should have been

Code:
Servo servo1, servo2;

Try increasing the delay in my second sketch to 200ms. I included the delay as kind of a button debounce but maybe it's not enough time.

Changes made. I have a switch with one leg connected to pin 4, no resistor, one leg to gnd. I am still getting no results when i press the button. Whilt typing this Robin replied... 2000ms added as per Robins advice. Still nothing is happening.

New code:

Code:
#include <Servo.h>
const uint8_t button = 4;
Servo servo1, servo2;
bool forward = false;

void setup() {
  pinMode(button, INPUT_PULLUP);
  servo1.attach(9);
  servo2.attach(10);
}

void loop() {
  if (digitalRead(button) == LOW && forward == false) {
    forward = true;
    servo1.write(170);
    servo2.write(10);
    delay(2000);  
  }
  else if (digitalRead(button) == LOW && forward == true) {
   forward = false;
   servo1.write(10);
   servo2.write(170);
   delay(2000);
  }  
}
10  Using Arduino / Programming Questions / Re: 2 Servo's sweep opposite directions via push button. on: February 13, 2014, 04:44:14 pm
You may think your prestidigitation is quick, though the Arduino is many times quicker, but light-speed it ain't.
No need to telegram Stockholm just yet.

Code:
if (digitalRead(button) == LOW && forward == false) {
    forward = true;
    servo1.write(170);
    servo2.write(10);
    delay(20); 
  }
  else if (digitalRead(button) == LOW && forward == true) {
   forward = false;
   servo1.write(10);
   servo2.write(170);
   delay(20);
  }
Move one way for 1/50th of a second, then move the opposite way for another 1/50th.
Rinse and repeat.

I dont understand, are you telling me that is what this code is doing?
11  Using Arduino / Programming Questions / Re: 2 Servo's sweep opposite directions via push button. on: February 13, 2014, 04:14:31 pm
Then I may have created a foundation for light speed travel. No longer interested in my illusion. Im aiming for a nobel prize here.
12  Using Arduino / Programming Questions / Re: 2 Servo's sweep opposite directions via push button. on: February 13, 2014, 03:26:56 pm
Sorry, it does absolutely nothing. I press a button, and nothing happens. I press a button again and nothing happens.

To recap, i wish to press a button and have 2 servos move in opposite directions for 1 sweep. Then press the button and the servos sweep back to the start position.
13  Using Arduino / Programming Questions / Re: 2 Servo's sweep opposite directions via push button. on: February 13, 2014, 02:36:14 pm
The irony of this situation is: Earlier, UKHelibob got annoyed with me because i didn't copy EXACTLY what he had asked me to. I learned my lesson so this time i copied things exactly.... It turns out to be wrong. I have made a guess as to what needs changing.

From cross referencing this is what I came up with, it doesn't work, i shall do more cross referencing...

Code:
#include <Servo.h>
const uint8_t button = 4;
Servo servo1;
Servo servo2;
bool forward = false;

void setup() {
  pinMode(button, INPUT_PULLUP);
  servo1.attach(9);
  servo2.attach(10);
}

void loop() {
  if (digitalRead(button) == LOW && forward == false) {
    forward = true;
    servo1.write(170);
    servo2.write(10);
    delay(20);  
  }
  else if (digitalRead(button) == LOW && forward == true) {
   forward = false;
   servo1.write(10);
   servo2.write(170);
   delay(20);
  }  
}
14  Using Arduino / Programming Questions / Re: 2 Servo's sweep opposite directions via push button. on: February 13, 2014, 01:24:06 pm
OK I'm home now.

Zoomkat, the code you provided does do something. I press the button and 1 servo sweeps once then back again. Its nice to be rid of that resistor though.

Vulture, your codes as seen in this quote, are giving me the error mentioned below the quote:
Zoomkat nailed it right on the head. Some of the "regulars" can be quite rude and condescending.

Try this code. I think it will do what you want.
Hook one side of the button to pin 4 and the other to ground. You will not need a resistor. Make sure your servos have their own 5v supply, not from the Arduino.
Code:
#include <Servo.h>
const uint8_t button = 4; //any pin you want as long as the code matches the hardware.
const uint8_t servo1 = 9;
const uint8_t servo2 = 10;
bool forward = false;

void setup() {
  pinMode(button, INPUT_PULLUP); //so you dont need a resistor.
  servo1.attach(9);
  servo2.attach(10);
}

void loop() {
  if (digitalRead(button) == LOW && forward == false) { //if pin 4 is grounded out by the switch, do something.
    forward = true; //reverse the direction
    for (uint8_t i = 10; i <= 170; i++) { //move servos 160 degrees and limit their movment.
      servo1.write(i);
      servo2.write(180 - i);
      delay(5);
    }
  }
  else if (digitalRead(button) == LOW && forward == true) {
    forward = false; //switch direction again.
    for (uint8_t i = 10; i <= 170; i ++) {
      servo1.write(180 - i);
      servo2.write(i);
      delay(5);
    }
  }
}

If you want the servos to move slower, increase the value in the delay() function. Try 10 or 15.
If you want your servos to move as fast as possible, use this code:


Code:
#include <Servo.h>
const uint8_t button = 4;
const uint8_t servo1 = 9;
const uint8_t servo2 = 10;
bool forward = false;

void setup() {
  pinMode(button, INPUT_PULLUP);
  servo1.attach(9);
  servo2.attach(10);
}

void loop() {
  if (digitalRead(button) == LOW && forward == false) {
    forward = true;
    servo1.write(170);
    servo2.write(10);
    delay(20); 
  }
  else if (digitalRead(button) == LOW && forward == true) {
   forward = false;
   servo1.write(10);
   servo2.write(170);
   delay(20);
  } 
}
sketch_feb13a.ino: In function 'void setup()':
sketch_feb13a:9: error: request for member 'attach' in 'servo1', which is of non-class type 'const uint8_t'
sketch_feb13a:10: error: request for member 'attach' in 'servo2', which is of non-class type 'const uint8_t'
sketch_feb13a.ino: In function 'void loop()':
sketch_feb13a:17: error: request for member 'write' in 'servo1', which is of non-class type 'const uint8_t'
sketch_feb13a:18: error: request for member 'write' in 'servo2', which is of non-class type 'const uint8_t'
sketch_feb13a:25: error: request for member 'write' in 'servo1', which is of non-class type 'const uint8_t'
sketch_feb13a:26: error: request for member 'write' in 'servo2', which is of non-class type 'const uint8_t'
15  Using Arduino / Programming Questions / Re: 2 Servo's sweep opposite directions via push button. on: February 13, 2014, 09:30:39 am
Ahhhh, I see. OK I will try them later.
Excitement returned.
Pages: [1] 2