Forum >Romeo Buttons 1-5 do not work correctly
ArduinoGeneral

Romeo Buttons 1-5 do not work correctly

userHead Roboburner 2011-07-06 11:37:24 6674 Views9 Replies
Hello fellow Arduianiadians,

I just received my romeo board a few days ago and already managed to do some simple tutorial work, everything worked very well thus far.

Today I tried the example code for the buttons 1-5 from the romeo manual, and it gave me 3-5 "right button ok, left button ok" etc. output in the serial console without me pressing a button at all.

Now when i just make an analogRead(0) to the port (with the jumper for the buttons in place of course) it gives me a constant output around 270 without a button pressed, and around 260 when a button is pressed (no matter which of the 5 buttons i press).

There is nothing else connected to my romeo except for the USB cable.

What can cause this?

Also i get this 270 reading on every other analog port i read out.

I'm open for any ideas / suggestions, but please remember I'm a beginner :-)

Greetings from the Roboburner
2012-02-06 18:50:38 ;)
Thank you for helping us to fix the problems of the sample code.
Also we'll update the wiki sketch soon.
userHeadPic Lauren
2012-02-05 00:27:13 First of all, I know that this topic is old, but I just got my RoMeo board and tried the Button 1-5 example from the wiki for this board and found that it did not work. I then come and look at this topic to see what was wrong. I expect other people will do the same so I want to post what I found wrong.

First the example has a typo. The line for Serial.println(msgs[keys]) has println with a capital I instead of a little l.

Second the example does not do a begin of the serial port. so no serial port output.

Here is my sketch after I fix the above and added some more output messages. I Upload this to the board and then started a Serial Monitor. I have also put an example output at theend of this messages. Hope this helps:

//
// Key message
char msgs[5][15] = {"Right Key OK ",
                    "Up Key OK    ",
                    "Down Key OK  ",
                    "Left Key OK  ",
                    "Select Key OK" };
char start_msg[15] = {"Start loop "};                   
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() {
    pinMode(13, OUTPUT);  //we'll use the debug LED to output a heartbeat
    Serial.begin(9600);
   
    /* Print that we made it here */
    Serial.println(start_msg);
}

void loop()
{
    adc_key_in = analogRead(7);    // read the value from the sensor 
    digitalWrite(13, HIGH);
    /* get the key */
    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){
                Serial.println(adc_key_in, DEC);
                Serial.println(msgs[key]);
            }
      }
    }
    digitalWrite(13, LOW);
}
// Convert ADC value to key number
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;
}


Here is the output as I pressed the keys:

Start loop
0
Right Key OK
146
Up Key OK   
331
Down Key OK 
507
Left Key OK 
742
Select Key OK
0
Right Key OK
userHeadPic gbcstott
2011-07-13 03:12:02 Analog Pin Input is a random number , very like to be a noise reading. But once you apply 0V or 5V on it, it should give you steady reading. userHeadPic R2D2C3PO
2011-07-13 01:50:16 Thanks Hector,

I'll test that! But in the online Manual from the Romeo it's said they are connected to Analog0? In addition I used a 1 to 1 copy of the sample code from that manual PDF :-(

I'll let you know if it reads out correctly on Analog07.


But another quick question, why does it read out those 270 values on the pins where nothing is connected to?

Thanks
userHeadPic Roboburner
2011-07-12 19:44:42 Hi Roboburner,

Why are you testing analogPin 0? Buttons 1 through 5 are connected to pin A07
You should be polling analogPin 7.



Please try this code and tell me what your output is on the serial monitor. If its still 270 then you have some problem with the board and you should ask the reseller for a replacement.

const int ledPin = 13;
const int key_S1_5 = 7;

int buttons_check(){
  int w = analogRead(key_S1_5);
  #define vS1 0
#define vS2 130
#define vS3 306
#define vS4 478
#define vS5 720
if ( w < vS2/2 ){
  Serial.print("button value: ");
  Serial.println(w);
  return 1;
}
if ( w < (vS3+vS2)/2 ){
  Serial.print("button value: ");
  Serial.println(w);
return 2;
}
if ( w < (vS4+vS3)/2 ){
  Serial.print("button value: ");
  Serial.println(w);
return 3;
}
if ( w < (vS5+vS4)/2 ){
  Serial.print("button value: ");
  Serial.println(w);
return 4;
}
if ( w < (1024+vS5)/2 ){
  Serial.print("button value: ");
  Serial.println(w);
return 5;
}
return 0;
}//End buttons_check()

void setup(){
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
delay(100);
Serial.println();
Serial.println("Romeo Tutorial 1 : the buttons");
Serial.println("The LED blinks once after the n button");
Serial.println("Do not forget to put the jumpers on");
Serial.println("S1-5");
Serial.println("press S1 .. S5");
}// End setup

void loop(){
int button = buttons_check();

if ( button >0 ) {
// A button has been pressed
Serial.print("button : S");
Serial.print( button );
delay(250); // Wait before next check
Serial.println();
}
}// End loop

Cheers,

Hector
userHeadPic Hector
2011-07-12 02:16:41 No one with any Info on this behavior?

Any Info from DFRobot maybe would be nice, because if my Board is damaged I would like to give it back within my 14-day money back guarantee i have with the reseller I bought it from.


userHeadPic Roboburner
2011-07-07 22:30:56 Hi Hector,

thank you for your reply and trying to help me.

I'm using the example code from the romeo manual with which the errors subscribed in my first post appear. After trying your code it looks the same. The jumpers are correctly in place, just doublechecked it. Buttons S6 + S7 work correct it's just with buttons 1-5.

It seems as if there is some kind of current flowing through the analog pins that disturbs the function of the buttons?

How are those (S1-S5) connected to the analog input? Are there any pull-up / pull-down resistors in place? As mentioned the analog reading is around 270 the whole time (for all analog in pins that is).

The code I use to read this is just a simple

analogRead(0) inside a serial output paired with a delay(250) after every read.

Serial console output looks like this (for analog 0 / Buttons S1-S5)

270
270
270
270
270
271
272
269
270
270
269
<---- Now I press a Button S1-S5 (it's really no difference which button I press to what happens here!!!)
259
258
260
261
255
258
260
<--- I release the above pressed Button
268
269
270
271
270
272
270
.... and so on

As mentioned, this happens exactly like that no matter which of the 5 (S1-S5) Buttons I press. If I put a pull-down resistor in place from ground to analog 0 then the reading is constantly "0" of course, but nothing at all happens when i press a button. When I put a pull up resistor in place the reading is as expected constantly 1023 but also nothing happens when pressing the buttons :-/

So is my analog input damaged? Is this because of the USB power from the PC so it is like some electrical disturbance to the analo readings? Are the buttons defect?

Any further help greatly appriciated :-)
userHeadPic Roboburner
2011-07-07 19:01:03 Roboburner,


Try the following code, also ensure that all 3 button jumpers are in place.

//Romeo Button testing.
//NOTE: please ensure all 3 jumpers for the buttons are placed, otherwise the buttons will not work properly

const int ledPin = 13;
// Buttons vars
const int key_S6 = 2;
const int key_S7 = 3;
const int key_S1_5 = 7;

int buttons_setup(){
// initialize digital buttons
pinMode(key_S6, INPUT);
pinMode(key_S7, INPUT);
}
int buttons_check(){
// This function checks the buttons and returns
// 0 if no button pressed
// Priorities: S7, S6, S1, S2, S3, S4, S5
if (digitalRead(key_S7)==0) return 7;
if (digitalRead(key_S6)==0) return 6;
int w = analogRead(key_S1_5);
#define vS1 0
#define vS2 130
#define vS3 306
#define vS4 478
#define vS5 720
if ( w < vS2/2 ){
  Serial.print("button value: ");
  Serial.println(w);
  return 1;
}
if ( w < (vS3+vS2)/2 ){
  Serial.print("button value: ");
  Serial.println(w);
return 2;
}
if ( w < (vS4+vS3)/2 ){
  Serial.print("button value: ");
  Serial.println(w);
return 3;
}
if ( w < (vS5+vS4)/2 ){
  Serial.print("button value: ");
  Serial.println(w);
return 4;
}
if ( w < (1024+vS5)/2 ){
  Serial.print("button value: ");
  Serial.println(w);
return 5;
}
return 0;
}//End buttons_check()

void setup(){
pinMode(ledPin, OUTPUT);
buttons_setup();
Serial.begin(9600);
delay(100);
Serial.println();
Serial.println("Romeo Tutorial 1 : the buttons");
Serial.println("The LED blinks once after the n button");
Serial.println("Do not forget to put the jumpers on");
Serial.println("S1-5, S6, S7");
Serial.println("press S1 .. S7");
}// End setup
void loop(){
int button = buttons_check();

if ( button >0 ) {
// A button has been pressed
Serial.print("button : S");
Serial.print( button );

  Serial.print(" ");
for( int i=1; i <= button; i++){
// Flashing button next No.
Serial.print(".");
digitalWrite(ledPin,HIGH); // On
delay(100); // 250 ms
Serial.print(" ");
digitalWrite(ledPin,LOW); // Off
delay(100); // 250 ms
}
delay(150); // Wait before next check
Serial.println();
}
}// End loop

Cheers,

Hector
userHeadPic Hector
2011-07-07 18:26:52 Hi Roboburner,

Have you set the jumpers for the switches in the correct position as it indicates in the Wiki?

Also, could you please post the sketch you are using?

Thanks,

Hector
userHeadPic Hector