Romeo All in one Controller DFR0004 - Button Press Example

Hello,

I’m very new to Arduino coding and am having a very hard time finding a functioning (and simple) example that uses the Romeo V1.3 (R3) on-board buttons to move a servo (or do anything else for that matter).
I do realize there is an example in the documentation on this page, but it’s a little over my head at the moment:
dfrobot.com/wiki/index.php/ … KU:DFR0004

I currently am using the following Pan/Tilt system with the micro-controller:
[h1]3.8:1 Aluminum PT785-S Pan / Tilt System[/h1]
robotshop.com/en/381-aluminu … ystem.html

My goal is to run a program via button press. Below is the code I understand so far:

//-----------------------------------------------------------------------------------------------

[code]
#include <Servo.h>
Servo servotilt;
Servo servopan;
//I need help declaring the button

void setup() {
servotilt.attach(0, 1050, 1950);
servopan.attach(1, 1050, 1950);
//I need help with initializing the button if necessary
}

void loop() {
//I need help with “On button detect do the following”:
servopan.write(90);
servotilt.write(90);
}[/code]
//-----------------------------------------------------------------------------------------------

I truly appreciate any help you can provide. Thanks!

Devan

Never start with too much or it will seem overwhelming. Start with one simple task at a time:

]Learn how to use a button to light up LED 13/:m]
]Learn how to use each of the buttons (i.e. different IO pins)/:m]
]Learn how to position a servo (servo library)/:m]
]Learn how to move the servo from one position to another/:m]
]Merge the button code and associate one button with one position, and another button with another position/:m]
]Have one button control a sequence of positions/:m]
]etc./:m]

After much tinkering I got lucky and found some code that works. Unfortunately I can’t say I understand all the ins and outs yet, but its a good start and the code works:

[code]
#include <Servo.h>
Servo servotilt;
Servo servopan;

int adc_key_val[5] ={ 30, 150, 360, 535, 760 };

int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;
void setup() {
//servotilt.attach(0, 1050, 1950);
//servopan.attach(1, 1050, 1950);
pinMode(13, OUTPUT);
}

void loop()
{
adc_key_in = analogRead(7); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey) { // if keypress is detected
delay(50); // wait for debounce time
adc_key_in = analogRead(7); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey) {
oldkey = key;
if (key >=0){
//ON KEY PRESS DO THE CODE BELOW
//-----------------------------
//LIGHT UP LED
digitalWrite(13, HIGH);
//MOVE SERVO
servopan.attach(1, 1050, 1950);
servotilt.attach(0, 1050, 1950);
servopan.write(120);
servotilt.write(120);
delay (5000);
servopan.detach();
servotilt.detach();
//-----------------------------
}
else {
//ON KEY RELEASE DO THE CODE BELOW
//-----------------------------
//Turn off LED
digitalWrite(13, LOW);
//MOVE SERVO
servopan.attach(1, 1050, 1950);
servotilt.attach(0, 1050, 1950);
servopan.write(90);
servotilt.write(90);
delay (5000);
servopan.detach();
servotilt.detach();
//-----------------------------
}
}
}

}
int get_key(unsigned int input)
{
int k;
for (k = 0; k < NUM_KEYS; k++)
{
if (input < adc_key_val[k])
{
return k;
}
}
if (k >= NUM_KEYS)
k = -1; // No valid key pressed
return k;
}[/code]