Pages: [1]   Go Down
Author Topic: Robot - problem  (Read 1225 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hy @ all,

i´m just a arduino beginner on his first steps...after solving a few problems with my arduino i wanted to build a small "robot".
For that i wanted to use an old RC-Toy Tank, i want to use a few parts of it and an arduino plus a few servos to build my little robot.

The RC Tank was disassembled so that we get the driving unit and all the electronic parts.  We don´t change anything in the driving section so that it can be operated from the Remote of the RC Tank (so that our robot can drive forward backward etc), the Tank Remote has another channel to control the upper part with the gun of the tank. Originally the Tank could rotate it´s upper part and fire small airsoft bullets. We disconnected the small dc motor which fires the bullets to get its signal. Then we connected the + to digital pin2 and - to ground. (tryed it before with a switch on pin2 and 5V+ and a resistor connected to ground and it worked fine). We hoped that we can get this signal to controll a few movements of the servos which are connected to the arduino. (everytime pin2 gets high is counted and on every count something happens)
The Servos are connected to digital pin 9-11, but only one of them was attached for the test today and they got their own power source.

now to my problem....

with a button connected to pin2 everything works fine....everytime i press the button my servo moves to the next position....but when i connect the r/c electronic to my arduino i only press the button of the r/c remote 1 time and the complete program cycles through, over and over again.

I already tested the r/c electronics with a led connected to the wires which go to the arduino and the result is that the led only turns on as long as i press the button...so maybe there is an error in my code ???


Code:
#include <Servo.h>
const int buttonPin = 2; 

Servo myservo;   // create servo object to control a servo
Servo arml;
Servo armr;

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState     = 0;  // current state of the button
int lastButtonState = 0;  // previous state of the button
int pos     = 0;  // variable to store the servo positions

void setup() {     // initialize serial communication:
  Serial.begin(9600);
  myservo.attach(9); // attaches the servo on pin 8 to the servo object
  myservo.write(0);  // set servo to zero
 arml.attach(10);
 armr.attach(11);
 arml.write(0);
 armr.write(0);
  pinMode(buttonPin, INPUT);  // initialize the button pin as a input
}

void loop() {
  buttonState = digitalRead(buttonPin); // read the pushbutton input pin
  if (buttonState != lastButtonState) { // compare buttonState to previous state

  if (buttonState == 1) {     // if the current state is 1 then the button
                              // went from off to on:
    buttonPushCounter++;
    Serial.println("on");
    Serial.print("number of button pushes:  ");
    Serial.println(buttonPushCounter, DEC);


                              // do something different depending on the
                              // range value:
    switch (buttonPushCounter) {



case 1:               // If the button count is at 1
Serial.println("1");     // print 1
myservo.write(180);   // tell servo to go to position 'pos'
delay(500);   // waits 0.5s for the servo to reach the position
arml.write(35);
        delay(500);
        armr.write(35);
        delay(500);
        break;


case 2:     // If the button count is at 2
Serial.println("2");     // print 2
  myservo.write(90);   // tell servo to go to position 'pos'
delay(500);   // waits 0.5s for the servo to reach the position
arml.write(60);
        delay(500);
        armr.write(60);
        delay(500);
break;



case 3: //if the button count is at 3
Serial.println("3");
myservo.write(0); // tell servo to go to position 'pos'
delay(500);      // waits 0.5s for the servo to reach the position
arml.write(45);
        delay(500);
        armr.write(45);
        delay(500);
break;



case 4:  // if the button count is 4
Serial.println("4");
myservo.write(144); // tell servo to go to position 'pos'
delay(500);    // waits 0.5s for the servo to reach the position
arml.write(75);
        delay(500);
        armr.write(75);
        delay(500);
break;



case 5:  //if the button count is 5
Serial.println("5");
myservo.write(180); // tell servo to go to position 'pos'
delay(500);      // waits 0.5s for the servo to reach the position
arml.write(90);
        delay(500);
        armr.write(90);
        delay(500);
break;



case 6:  //if the button count is 6
Serial.println("6");
                          // reset everything.
                        // set servo to zero
myservo.write(0); // tell servo to go to position '0'
delay(1000);      // waits 1 second for the servo to reach the position
arml.write(0);
        delay(500);
        armr.write(0);
        delay(500);

                          // reset the push button count to 0
buttonPushCounter = 0 ;// Now everything is set back to the way we started.
  break;


  }
   }
    }
     }

 

Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8084
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

After you check for (buttonState != lastButtonState) your forgot to say:

Code:
lastButtonState = buttonState;
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks a lot !! that already helps a bit  smiley

but when i add that line, i got the result that the servo turns to the first position when i press the button on the rc remote, the second time i press the button nothing more happens instead of turning the servo to its next position ??

and here is the updated code

Code:
#include <Servo.h>
const int buttonPin = 2; 

Servo myservo;   // create servo object to control a servo
Servo arml;
Servo armr;

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState     = 0;  // current state of the button
int lastButtonState = 0;  // previous state of the button
int pos     = 0;  // variable to store the servo positions

void setup() {     // initialize serial communication:
  Serial.begin(9600);
  myservo.attach(9); // attaches the servo on pin 8 to the servo object
  myservo.write(0);  // set servo to zero
 arml.attach(10);
 armr.attach(11);
 arml.write(0);
 armr.write(0);
  pinMode(buttonPin, INPUT);  // initialize the button pin as a input
}

void loop() {
  buttonState = digitalRead(buttonPin); // read the pushbutton input pin
  if (buttonState != lastButtonState) { // compare buttonState to previous state
lastButtonState = buttonState;
  if (buttonState == 1) {     // if the current state is 1 then the button
                              // went from off to on:
    buttonPushCounter++;
    Serial.println("on");
    Serial.print("number of button pushes:  ");
    Serial.println(buttonPushCounter, DEC);


                              // do something different depending on the
                              // range value:
    switch (buttonPushCounter) {



case 1:               // If the button count is at 1
Serial.println("1");     // print 1
myservo.write(180);   // tell servo to go to position 'pos'
delay(500);   // waits 0.5s for the servo to reach the position
arml.write(35);
        delay(500);
        armr.write(35);
        delay(500);
        break;


case 2:     // If the button count is at 2
Serial.println("2");     // print 2
  myservo.write(90);   // tell servo to go to position 'pos'
delay(500);   // waits 0.5s for the servo to reach the position
arml.write(60);
        delay(500);
        armr.write(60);
        delay(500);
break;



case 3: //if the button count is at 3
Serial.println("3");
myservo.write(0); // tell servo to go to position 'pos'
delay(500);      // waits 0.5s for the servo to reach the position
arml.write(45);
        delay(500);
        armr.write(45);
        delay(500);
break;



case 4:  // if the button count is 4
Serial.println("4");
myservo.write(144); // tell servo to go to position 'pos'
delay(500);    // waits 0.5s for the servo to reach the position
arml.write(75);
        delay(500);
        armr.write(75);
        delay(500);
break;



case 5:  //if the button count is 5
Serial.println("5");
myservo.write(180); // tell servo to go to position 'pos'
delay(500);      // waits 0.5s for the servo to reach the position
arml.write(90);
        delay(500);
        armr.write(90);
        delay(500);
break;



case 6:  //if the button count is 6
Serial.println("6");
                          // reset everything.
                        // set servo to zero
myservo.write(0); // tell servo to go to position '0'
delay(1000);      // waits 1 second for the servo to reach the position
arml.write(0);
        delay(500);
        armr.write(0);
        delay(500);

                          // reset the push button count to 0
buttonPushCounter = 0 ;// Now everything is set back to the way we started.
  break;


  }
   }
    }
     }

Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8084
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code looks fine to me.  So the last thing that comes out on the serial monitor is '1'?  Either I missed something in the code, or your problem is in hardware, or you ran out of memory.  Try removing the "number of button pushes:   " string to free up some memory.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So the last thing that comes out on the serial monitor is '1'?

yes, totally right

First press of the button - serial responds with the count 1 and the servo moves to the first position
second press - nothing happens on serial monitor - servo stands still on position 1

something curious which i noticed while trying out a few things to correct the problem....

if i turn the r/c electronic off and on again and press the button for another time - the servo moves to the second position and the serial monitor show me the 2nd count....but again only 1 time...another press of the button does nothing until i redo the complete power off/on procedure....
Logged

0
Offline Offline
Shannon Member
****
Karma: 161
Posts: 10445
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Perhaps the RC output needs a pull-down resistor?
Logged

[ I won't respond to messages, use the forum please ]

Pages: [1]   Go Up
Jump to: