Learning to program the BotBoarduino

Finally had some free time and took my BotBoarduino for a testdrive. Just a small program to controll a servo using buttons.
I haven’t used any form of C in a very long time and wanted to know if this was ok. It seems to do what i wanted it to.

[code]// Controlled Sweep
// Sweep servo Left/Right using a button and center by pressing both

#include <Servo.h>

Servo MyServo ;

int BUTTON_R = 2; // pushbutton pin rotate right
int BUTTON_L = 3; // pushbutton pin rotate left
int HornPos = 90; // Servo position set to center
int BtnState1 = 0; // Pushbutton status
int BtnState2 = 0;

void setup() {
MyServo.attach(12); // attaches the servo on pin 12 to the servo object
pinMode(BUTTON_R, INPUT); // initialize the pushbutton pins as an input:
pinMode(BUTTON_L, INPUT);
}

void loop(){
BtnState1 = digitalRead(BUTTON_R); // read the state of the pushbuttons
BtnState2 = digitalRead(BUTTON_L);

delay(30); // Debounce Buttons

if(BtnState1 == HIGH && BtnState2 == LOW){
HornPos = (HornPos + 1) ; // Rotate right one step
MyServo.write(HornPos);
}
else if(BtnState2 == HIGH && BtnState1 == LOW){
HornPos = (HornPos - 1) ; // Rotate left one step
MyServo.write(HornPos);
}
else if(BtnState2 == HIGH && BtnState1 == HIGH){
HornPos = 90; // Rotate to center
MyServo.write(HornPos);
}
}[/code]

Now I want to try to use a PS2 controller to move the servo.

Congratulations! Let us know how it goes!

Okay I really only see one thing that’s not going to produce the effect you want.

[code] BtnState1 = digitalRead(BUTTON_R); // read the state of the pushbuttons
BtnState2 = digitalRead(BUTTON_L);

delay(30); // Debounce Buttons
[/code]

This delay isn’t debouncing the buttons. It’s simply delaying the program. An example of a basic debounce looks something like this:

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }
 
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    buttonState = reading;
  }
 
  // set the LED using the state of the button:
  digitalWrite(ledPin, buttonState);

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
}

This example comes from the official Arduino website :arduino.cc/it/Tutorial/Debounce

If this doesn’t quite make sense to you i would suggest looking at the Bounce library that handles the whole process for you. This library can be found here: arduino.cc/playground/Code/Bounce

Best of luck to you!

Thanks for the info on the debounce. I still need to rap my head around it. got me confused. when just a little delay did the trick. :confused:
I was able to controll a servo using the PS2 wireless controller :smiley:

[code]// Controlled Sweep
// Sweep servo Left using PAD left pad left or Square button
// Sweep servo rigth using PAD leftt pad right or Circle button
// Return to center using PAD leftt pad up or X button
#include <Servo.h>
#include <PS2X_lib.h> //V1.6 http://www.billporter.info/playstation-2-controller-arduino-library-v1-0/

#define PS2_DAT 6 // Borrowed from Kurt makes it easier to read
#define PS2_CMD 7
#define PS2_SEL 8
#define PS2_CLK 9

Servo MyServo ;
PS2X ps2x ;

int HornPos = 90; // Servo position set to center
int error = 0;
byte type = 0;
byte vibrate = 0;

void setup() {
Serial.begin(57600);

//setup pins and settings:
//GamePad(clock, command, attention, data, Pressures?, Rumble?)
//check for error

error = ps2x.config_gamepad(PS2_CLK,PS2_CMD,PS2_SEL,PS2_DAT, true, true);
MyServo.attach(12); // attaches the servo on pin 12 to the servo object

if(error == 0){
Serial.println(“Found Controller, configured successful”);
}
else if(error == 1){
Serial.println(“No controller found”);
}
else if(error == 2){
Serial.println(“Controller found but not accepting commands.”);
}

type = ps2x.readType();

 switch(type) {
   case 0:
    Serial.println("Unknown Controller type");
   break;
   case 1:
    Serial.println("DualShock Controller Found");
   break;
   case 2:
    Serial.println("GuitarHero Controller Found");
   break;
 }

}

void loop(){

if(error == 1) //skip loop if no controller found
return;
else { //DualShock Controller

ps2x.read_gamepad();          //read controller and set large motor to spin at 'vibrate' speed
  
if(ps2x.Button(PSB_RED)){             //will be TRUE if button was JUST pressed
     Serial.println("Circle just pressed");
       ++HornPos ;
       MyServo.write(HornPos);
} 
if(ps2x.Button(PSB_PINK)){             //will be TRUE if button was JUST released
     Serial.println("Square just released");     
      --HornPos; // Rotate left one step
      MyServo.write(HornPos); 
}
if(ps2x.Button(PSB_BLUE)) {           //will be TRUE if button was JUST pressed OR released
     Serial.println("X just changed");    
      HornPos = 90; // Rotate to center
      MyServo.write(HornPos); 
} 

if(ps2x.Button(PSB_PAD_RIGHT)){ //will be TRUE if button was JUST pressed
Serial.print("Right held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
++HornPos ;
MyServo.write(HornPos);
}
if(ps2x.Button(PSB_PAD_LEFT)){ //will be TRUE if button was JUST released
Serial.print("LEFT held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
–HornPos; // Rotate left one step
MyServo.write(HornPos);
}
if(ps2x.Button(PSB_PAD_UP)) { //will be TRUE if button was JUST pressed OR released
Serial.print("Up held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
HornPos = 90; // Rotate to center
MyServo.write(HornPos);
}
}

delay(20);

}[/code]

Hi,
The BotBoarduino hasn’t been to bad of a learning curve. been having a good time with it, and no puff of smoke yet. I would like to get a airsoft R/C tank gun working. I read through some of the threads and found the PS2 Biped BRAT Mech Tutorial. I just ordered a couple of Picoswitchs as i wasnt able to find a easy work around. I saw a real helpful photo Kurte had posted of how his brat was wire. But haven’t been able to find it again. Any pointers/advice while waiting for my hardware.

Thanks

Maybe look at this post? viewtopic.php?t=5221

Kurt

I got a couple of PicoSwitches, I wired them up like the diagram PICOSW.jpg I must not be understanding something :confused: I think it has to do with the PICOSWITCH because it just flashes. This is the code I’m working with

[code]#include <Servo.h>
#include <PS2X_lib.h> //V1.6 http://www.billporter.info/playstation-2-controller-arduino-library-v1-0/

#define SOUND_PIN 5
#define PS2_DAT 6
#define PS2_CMD 7
#define PS2_SEL 8
#define PS2_CLK 9

Servo MyServo ;
PS2X ps2x ;

int MtrSW = 10; // PICOSWITCH
int GunSW = 11;
int error = 0;
byte type = 0;
byte vibrate = 0;

void setup() {
Serial.begin(57600);
pinMode(MtrSW, OUTPUT); // initialize the pushbutton pins as an input:
pinMode(GunSW, INPUT);

error = ps2x.config_gamepad(PS2_CLK,PS2_CMD,PS2_SEL,PS2_DAT, true, true); //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
MyServo.attach(12); // attaches the servo on pin 12 to the servo object

if(error == 0){
Serial.println(“Found Controller, configured successful”);
}
else if(error == 1){
Serial.println(“No controller found”);
}
else if(error == 2){
Serial.println(“Controller found but not accepting commands.”);
}

type = ps2x.readType();

 switch(type) {
   case 0:
    Serial.println("Unknown Controller type");
   break;
   case 1:
    Serial.println("DualShock Controller Found");
   break;
   case 2:
    Serial.println("GuitarHero Controller Found");
   break;
 }

}

void loop(){

if(error == 1) //skip loop if no controller found
return;
else { //DualShock Controller

ps2x.read_gamepad();          //read controller and set large motor to spin at 'vibrate' speed
  
if(ps2x.Button(PSB_GREEN)) {           //will be TRUE if button was JUST pressed OR released
     Serial.println("Triangle pressed BANG!");    
      digitalWrite(GunSW, HIGH);
      delay(1000);
      digitalWrite(GunSW, LOW);
} 

}

delay(20);

}[/code]

I may be wrong but my recollection is that I used servo type signals to control a picoswitch, with values of something like turn on with pulse width like 2000us and off 1000us… Values are not specific, just far enough from the center 1500…

Kurt

Hi kurt,
You put me on the right track. Making this change sends the gun motor spining, but i still don have full control

if(ps2x.Button(PSB_GREEN)) { //will be TRUE if button was JUST pressed OR released Serial.println("Triangle pressed BANG!"); MyServo.write(++HornPos); delay(100); }
Thanks

WOW! I’ve really got myself confused :confused: I wasn’t able to find much on the PicoSwitch and arduino. I found that you can use analogWrite() for PWM, but didn’t do much for me. Just pulled all my wires off the board. maybe a fresh start later might help.

If it were me, I would try it maybe like:

if(ps2x.ButtonPressed(PSB_GREEN)) { //will be TRUE if button was JUST Pressed Serial.println("Triangle pressed BANG!"); MyServo.write(2250); delay(100); } if(ps2x.ButtonReleased(PSB_GREEN)) { //will be TRUE if button was JUST Released Serial.println("Triangle pressed BANG!"); MyServo.write(750); delay(100); }

Might not work, but maybe worth a try… As I mentioned earlier, I have not used one of these for a long time. Later I used on of the switches from Basic Micro. With those you have other options to control turning it on, including serial, and simple TTL on/off.

Kurt

Kurte, it didn’t work me. I think i need to step back and first understand how the picoswitch works. Then i can proceed (it’s just me putting the cart before the horse). Thanks for the help.

Yep, Not sure what to tell you here. Their documentation on it is rather sparse. dimensionengineering.com/products/picoswitch

Their documents only show it being hooked up to be controlled by an RC receiver. Been awhile since I played with one. May have to dig one out and give it a whirl…

Kurt

I was thinking it should run as a relay? I found this on line but it uses a potentiometer. I need to pick up a copy of the adruino cookbook on the way home.

Kurte is right in the fact that the picoswitch you have responds to a servo pulse, not digital logic. The code kurte posted earlier should make the relay do something… Keep in mind that the relay will not control the speed of the motor as it is simply an On/Off relay.

Drop a servo in instead of the Pico switch, and see if you can get it to go back and forth between the two points. If it works, then the Pico should too.

Alan KM6VV

Kurt,
the code turns the picoswitch on and off. I had to change the values. the ones Ichoose seem to work well for me. I tried as Alan suggested and replace the Picoswitch with a servo. The servo worked so i tried changing the servo position values. Now I just need to work on the switch part now.

[code]// Controlled Sweep
// Sweep servo Left/Right using a button and center by pressing both

#include <Servo.h>
#include <PS2X_lib.h> //V1.6 http://www.billporter.info/playstation-2-controller-arduino-library-v1-0/

#define SOUND_PIN 5
#define PS2_DAT 6
#define PS2_CMD 7
#define PS2_SEL 8
#define PS2_CLK 9

Servo MyServo ;
PS2X ps2x ;

int HornPos = 90; // Servo position set to center
int GunPin = 12; // Pushbutton status
int MtrSW = 0;
int error = 0;
byte type = 0;
byte vibrate = 0;

void setup() {
Serial.begin(57600);
pinMode(MtrSW, OUTPUT);

error = ps2x.config_gamepad(PS2_CLK,PS2_CMD,PS2_SEL,PS2_DAT, true, true); //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
MyServo.attach(12); // attaches the servo on pin 12 to the servo object

if(error == 0){
Serial.println(“Found Controller, configured successful”);
}
else if(error == 1){
Serial.println(“No controller found”);
}
else if(error == 2){
Serial.println(“Controller found but not accepting commands.”);
}

type = ps2x.readType();

 switch(type) {
   case 0:
    Serial.println("Unknown Controller type");
   break;
   case 1:
    Serial.println("DualShock Controller Found");
   break;
   case 2:
    Serial.println("GuitarHero Controller Found");
   break;
 }

}

void loop(){

if(error == 1) //skip loop if no controller found
return;
else { //DualShock Controller

ps2x.read_gamepad();          //read controller and set large motor to spin at 'vibrate' speed
  
if(ps2x.ButtonPressed(PSB_GREEN)) {           //will be TRUE if button was JUST Pressed
     Serial.println("Triangle pressed BANG!");   
      MyServo.write(1700);
      delay(100);

}
if(ps2x.ButtonReleased(PSB_GREEN)) { //will be TRUE if button was JUST Released
Serial.println("Triangle released ");
MyServo.write(1400);
delay(100);
}
}
delay(20);
}[/code]

I was able to get it working


This the code I used

[code]// RC TANK AIRSOFT GUN
// USING PS2 CONTROLLER
#include <Servo.h>
#include <PS2X_lib.h> //V1.6 http://www.billporter.info/playstation-2-controller-arduino-library-v1-0/

#define SOUND_PIN 5
#define PS2_DAT 6
#define PS2_CMD 7
#define PS2_SEL 8
#define PS2_CLK 9

Servo MyServo ;
PS2X ps2x ;

int ValOn = 130; // Value to turn Picoswitch on
int ValOff = 50; // Value to turn Picoswitch off
int GunPin = 12; // Picoswitch attached to board
int error = 0;
byte type = 0;
byte vibrate = 0;

void setup() {
Serial.begin(57600);

error = ps2x.config_gamepad(PS2_CLK,PS2_CMD,PS2_SEL,PS2_DAT, true, true); //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
MyServo.attach(12); // attaches the servo on pin 12 to the servo object

if(error == 0){
Serial.println(“Found Controller, configured successful”);
}
else if(error == 1){
Serial.println(“No controller found”);
}
else if(error == 2){
Serial.println(“Controller found but not accepting commands.”);
}

type = ps2x.readType();

 switch(type) {
   case 0:
    Serial.println("Unknown Controller type");
   break;
   case 1:
    Serial.println("DualShock Controller Found");
   break;
 }

}

void loop(){

if(error == 1) //skip loop if no controller found
return;
else { //DualShock Controller

ps2x.read_gamepad();          //read controller and set large motor to spin at 'vibrate' speed
  
if(ps2x.ButtonPressed(PSB_GREEN)) {           //will be TRUE if button was JUST Pressed
     Serial.println("Triangle pressed BANG!");   
      MyServo.write(130);
      delay(1300); // Adjust for fire cycle
}
if(ps2x.ButtonReleased(PSB_GREEN)) {           //will be TRUE if button was JUST Released
     Serial.println("Triangle released ");   
      MyServo.write(50);
      delay(100);
}

}
delay(20);
}[/code]
I used a seperate 6.V power source for the gun and USB power for the board and PS2.
Maybe some can explain this to me. As you can see in the photo, I didnt need to connect the switch (green and black wire) to get it to work. Am I missing something?

Thanks for all your help

Again from Fish’s post: viewtopic.php?t=5221

When you press the button, the gun starts to fire, once it does it triggers the switch. I believe that once you trigger to fire the gun, his code would continue the motor running, until the gun actually fires.

Kurt