Go Down

Topic: Little Help Guys! Arduino Programming (Read 1 time) previous topic - next topic

kelvinsm

I'm making a project that will be 3x4 keypad for android phones using an arduino clone 328 and a bluetooth shield. If I am pushed switch two once it will type '2' on the android and it if I pushed it second time it will have an output 'a'. here's my code
Code: [Select]
char someChar;
  if(digitalRead(sensor)== HIGH){
   
   someChar = '1';
   incr++;
   if(incr == 1){
    someChar = 'a';

   }

    if(incr == 2){
    someChar = 'b';
   
   }
    if(incr == 3){
    someChar = 'c';
   
   }
   
 
  mySerial.print(someChar);
 
  }
  delay(100);
 

but what it does
https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-snc6/230214_2577134325670_286155044_n.jpg


PaulS

Code: [Select]
  if(digitalRead(sensor)== HIGH){
You went to mouser's site and ordered a sensor? It came in a box labeled "sensor"?

Quote
here's my code

I don't think so. No setup() function. No loop() function.

PeterH

Are you trying to reproduce the behaviour common on phones, where you can type 2/a/b/c by repeating the same button, with delays used to separate similar characters? If so you will need quite a lot of extra logic to count up repeat presses of the same key and apply timeouts, and cycle the output character through the various possible options. Having the Arduino output a different character for each button press isn't sufficient to achieve this, and actually makes the implementation (on the Android, I suppose?) much harder.

I only provide help via the forum - please do not contact me for private consultancy.

kelvinsm

here's my full code
Code: [Select]

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);
int incr = 0;
char sensor = 4;


void setup()  
{
 // use the ba ud rate your bluetooth module is configured to
 // not all baud rates are working well, i.e. ATMEGA168 works best with 57600
 Serial.begin(9600);
  Serial.println("Come at me baby!");
  mySerial.begin(9600);
  mySerial.println("Allright");
 // we initialize analog pin 5 as an input pin
 pinMode(sensor, INPUT);
}

void loop()
{
 
 // read input pin and send result to Android
 char someChar;
 if(digitalRead(sensor)== HIGH){
 
  someChar = '1';
  incr++;
  if(incr == 1){
   someChar = 'a';

  }

   if(incr == 2){
   someChar = 'b';
   
  }
   if(incr == 3){
   someChar = 'c';
 
  }
 
 
 mySerial.print(someChar);
 
 }
 delay(100);
}

kelvinsm


Are you trying to reproduce the behaviour common on phones, where you can type 2/a/b/c by repeating the same button, with delays used to separate similar characters? If so you will need quite a lot of extra logic to count up repeat presses of the same key and apply timeouts, and cycle the output character through the various possible options. Having the Arduino output a different character for each button press isn't sufficient to achieve this, and actually makes the implementation (on the Android, I suppose?) much harder.




Thanks for replying to my post PeterH! Yes I'm trying to reproduce the same behaviour common phones, and yes the phone is an android phones. To be honest, I don't fully understand what you have suggested to me(especially with timeouts and cycle stuffs) because I'm using and learning arduino for only about two weeks. It is much harder than?

PaulS

Quote
To be honest, I don't fully understand what you have suggested to me(especially with timeouts and cycle stuffs) because I'm using and learning arduino for only about two weeks.

The problem that you need to solve is one of understanding whether or not the same switch is being pressed twice (2 -> 'a') quickly, or being pressed twice (2, 2). You need to also deal with other keys being pressed in the meantime. Pressing 2, 1, 2 quickly should NOT result in a, 1.

This really is not a beginner project.

PeterH

Suggest you step back from the problem and describe what you're trying to achieve. On one side of your solution you have a keypad that looks like a conventional phone keypad. It looks like you want to use this like a conventional phone keypad. Ignoring for the moment the magic that has to occur to figure out what character is being generated based on key presses, what is ultimately going to happen with the result - what's it for?
I only provide help via the forum - please do not contact me for private consultancy.

WizenedEE


Quote
To be honest, I don't fully understand what you have suggested to me(especially with timeouts and cycle stuffs) because I'm using and learning arduino for only about two weeks.

The problem that you need to solve is one of understanding whether or not the same switch is being pressed twice (2 -> 'a') quickly, or being pressed twice (2, 2). You need to also deal with other keys being pressed in the meantime. Pressing 2, 1, 2 quickly should NOT result in a, 1.

This really is not a beginner project.


There doesn't need to be a timeout. So if you wanted to type "bb" you'd press "222<right>222" rather than "222<pause>222"

something like this:
Code: [Select]

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);
int incr = 0;
char btn2 = 4;


void setup() 
{
    // use the ba ud rate your bluetooth module is configured to
    // not all baud rates are working well, i.e. ATMEGA168 works best with 57600
    Serial.begin(9600);
     Serial.println("Come at me baby!");
     mySerial.begin(9600);
     mySerial.println("Allright");
    // we initialize analog pin 5 as an input pin
    pinMode(sensor, INPUT);
}

void loop()
{
   
    char someChar;
    static char lastBtn = -1;
    static char lastChar = 0;
    static int numPresses = 0;
    if(digitalRead(btn2) == HIGH) {
        if (lastChar != 2) {
            if (lastChar != 0) mySerial.print(lastChar);
            numPresses = 0;
            lastBtn = 2;
        }
        someChar = '1';
        lastChar = 'a' + numPresses;
        numPresses++;
    }
    delay(100);
}

kelvinsm



Quote
To be honest, I don't fully understand what you have suggested to me(especially with timeouts and cycle stuffs) because I'm using and learning arduino for only about two weeks.

The problem that you need to solve is one of understanding whether or not the same switch is being pressed twice (2 -> 'a') quickly, or being pressed twice (2, 2). You need to also deal with other keys being pressed in the meantime. Pressing 2, 1, 2 quickly should NOT result in a, 1.

This really is not a beginner project.


There doesn't need to be a timeout. So if you wanted to type "bb" you'd press "222<right>222" rather than "222<pause>222"

something like this:
Code: [Select]

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);
int incr = 0;
char btn2 = 4;


void setup() 
{
    // use the ba ud rate your bluetooth module is configured to
    // not all baud rates are working well, i.e. ATMEGA168 works best with 57600
    Serial.begin(9600);
     Serial.println("Come at me baby!");
     mySerial.begin(9600);
     mySerial.println("Allright");
    // we initialize analog pin 5 as an input pin
    pinMode(sensor, INPUT);
}

void loop()
{
   
    char someChar;
    static char lastBtn = -1;
    static char lastChar = 0;
    static int numPresses = 0;
    if(digitalRead(btn2) == HIGH) {
        if (lastChar != 2) {
            if (lastChar != 0) mySerial.print(lastChar);
            numPresses = 0;
            lastBtn = 2;
        }
        someChar = '1';
        lastChar = 'a' + numPresses;
        numPresses++;
    }
    delay(100);
}



Thanks, I'll try this code.

kelvinsm


Quote
To be honest, I don't fully understand what you have suggested to me(especially with timeouts and cycle stuffs) because I'm using and learning arduino for only about two weeks.

The problem that you need to solve is one of understanding whether or not the same switch is being pressed twice (2 -> 'a') quickly, or being pressed twice (2, 2). You need to also deal with other keys being pressed in the meantime. Pressing 2, 1, 2 quickly should NOT result in a, 1.

This really is not a beginner project.


Yap, I haven't really thought that this is a beginner project.

kelvinsm


Suggest you step back from the problem and describe what you're trying to achieve. On one side of your solution you have a keypad that looks like a conventional phone keypad. It looks like you want to use this like a conventional phone keypad. Ignoring for the moment the magic that has to occur to figure out what character is being generated based on key presses, what is ultimately going to happen with the result - what's it for?


I'm making a cellphone for the blind. It is a phone that has bluetooth, gps, mp3 and can receive and send text messages and calls. It should also have a voice prompt that will read text messages from the phone. And it should also have a braille keypad. It was suggested to us to make a bluetooth keypad that will serve as the main controller for the android phone for the blind. The keypad that will be using is a 3x4 keypad and the mcu we're usin is an arduino clone atmega328.

PeterH

On my phone, when I'm using the keypad to enter text I press '2' and it shows 'a', I press '2' again and the 'a' changes to a 'b'. Press again and it changes to a 'c'. Not being familiar enough with it to compose a text without looking, I need this feedback to know what character is selected so I can keep pecking away at the keys until it shows what I want. Are you going to provide similar feedback in your system? If so, how?
I only provide help via the forum - please do not contact me for private consultancy.

kelvinsm


On my phone, when I'm using the keypad to enter text I press '2' and it shows 'a', I press '2' again and the 'a' changes to a 'b'. Press again and it changes to a 'c'. Not being familiar enough with it to compose a text without looking, I need this feedback to know what character is selected so I can keep pecking away at the keys until it shows what I want. Are you going to provide similar feedback in your system? If so, how?


yep, I think that's the best way to make a keypad, just the way you said it, "I press '2' and it shows 'a', I press '2' again and the 'a' changes to a 'b'. Press again and it changes to a 'c'.". I'm not sure how will I do it. But I'm thinking having many If statements.

PaulS

Quote
"I press '2' and it shows 'a', I press '2' again and the 'a' changes to a 'b'. Press again and it changes to a 'c'.". I'm not sure how will I do it. But I'm thinking having many If statements.

Keep in mind that pressing another key in between needs to reset the count of key presses. And, you need to deal, with some keys, like 3, where the user might want to produce "ee" that even the same key pressed more than once may, or may not, be trying to generate a new letter.

That is why the timing between keypresses is important.

kelvinsm


There doesn't need to be a timeout. So if you wanted to type "bb" you'd press "222<right>222" rather than "222<pause>222"

something like this:
Code: [Select]

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);
int incr = 0;
char btn2 = 4;


void setup() 
{
    // use the ba ud rate your bluetooth module is configured to
    // not all baud rates are working well, i.e. ATMEGA168 works best with 57600
    Serial.begin(9600);
     Serial.println("Come at me baby!");
     mySerial.begin(9600);
     mySerial.println("Allright");
    // we initialize analog pin 5 as an input pin
    pinMode(sensor, INPUT);
}

void loop()
{
   
    char someChar;
    static char lastBtn = -1;
    static char lastChar = 0;
    static int numPresses = 0;
    if(digitalRead(btn2) == HIGH) {
        if (lastChar != 2) {
            if (lastChar != 0) mySerial.print(lastChar);
            numPresses = 0;
            lastBtn = 2;
        }
        someChar = '1';
        lastChar = 'a' + numPresses;
        numPresses++;
    }
    delay(100);
}

[/quote]

WizenedEE, what happen is,

https://fbcdn-sphotos-h-a.akamaihd.net/hphotos-ak-ash4/314674_2587553506143_471668955_n.jpg


Go Up