Little Help Guys! Arduino Programming

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

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

  if(digitalRead(sensor)== HIGH){

You went to mouser's site and ordered a sensor? It came in a box labeled "sensor"?

here's my code

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

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.

here's my full code

#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);
}

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.

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?

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.

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?

PaulS:

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 "222222" rather than "222222"

something like this:

#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);
}

WizenedEE:

PaulS:

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 "222222" rather than "222222"

something like this:

#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.

PaulS:

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.

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'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.

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?

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?

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.

"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.

There doesn't need to be a timeout. So if you wanted to type "bb" you'd press "222222" rather than "222222"

something like this:

#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

PaulS:
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.

Yes, can you give me example codes for timing that is related to my problem?

There doesn't need to be a timeout. So if you wanted to type "bb" you'd press "222222" rather than "222222"

I think that would be "2222", wouldn't it?

Then, to type "be", you'd type "233". Do you really want to hit the right key after every letter?

PaulS:

There doesn't need to be a timeout. So if you wanted to type "bb" you'd press "222222" rather than "222222"

I think that would be "2222", wouldn't it?

I believe OP originally said he wanted it to display 2 if pressed once. It is rather irrelevant though.

Then, to type "be", you'd type "233". Do you really want to hit the right key after every letter?

Well in the code I posted, it would display the currently selected letter after anything other than a 2 was pressed... So "be" would be "2233"

WizenedEE, what happen is,

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

Can you post your exact code?

here

#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
    
}

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);
}
    // we initialize analog pin 5 as an input pin

We do? Please don't post code using that invisible font.

    if(digitalRead(btn2) == HIGH) {

This will NOT work, in any serious program. You need to detect transitions, not states. You need to know when the switch becomes pressed, not is pressed.

What, exactly, is someChar doing?

How IS the switch wired to pin 4?