1

再会!我真的不知道如何提出正确的问题,但我会尝试彻底解释这种情况。

我试图为安卓手机制作一个 12 键键盘,我使用了迷你克隆版的 arduino Atmeg168、一个 3x4 键盘和一个蓝牙屏蔽,老实说,我还没有真正成功地显示所有的字母。我正在使用一个名为蓝牙 SPP 的 Android 应用程序来接收蓝牙传输的串行数据,我想要实现的是能够将字符或发送到 android 文本框的字符发送到文本框。可能吗?这是我一直在使用的代码

#include <Keypad.h>
#include <SoftwareSerial.h>
#include <ctype.h>

SoftwareSerial mySerial(2, 3);
const byte ROWS = 4; 
const byte COLS = 3; 
int digitsEntered = 0; //number of digits entered

char alphaKeys[ROWS][COLS] = {
    { '.','a','d' },
    { 'g','j','m' },
    { 'p','t','w' },
    { '*',' ','#' }
};

char alpha2Keys[ROWS][COLS] = {
    { ',','b','e' },
    { 'h','k','n' },
    { 'q','u','x' },
    { '*','s','#' }
};

char alpha3Keys[ROWS][COLS] = {
    { '?','c','f' },
    { 'i','l','o' },
    { 'r','v','y' },
    { '*','z','#' }
};

char numberKeys[ROWS][COLS] = {
    { '1','2','3' },
    { '4','5','6' },
    { '7','8','9' },
    { '*','0','#' }
};

boolean alpha = false; 
boolean alpha2 = false;
boolean alpha3 = false;


byte rowPins[ROWS] = { 5, 10, 9, 7 };
byte colPins[COLS] = {6, 4, 8 };

Keypad numpad( makeKeymap(numberKeys), rowPins, colPins, sizeof(rowPins), sizeof(colPins) );
Keypad ltrpad( makeKeymap(alphaKeys), rowPins, colPins, sizeof(rowPins), sizeof(colPins) );
Keypad ltr( makeKeymap(alpha2Keys), rowPins, colPins, sizeof(rowPins), sizeof(colPins) );
Keypad letters( makeKeymap(alpha3Keys), rowPins, colPins, sizeof(rowPins), sizeof(colPins) );


unsigned long startTime;
const byte ledPin = 13;        
const byte led = 12;
void setup() {

pinMode (4, INPUT);//Column 1
pinMode (6, INPUT);//Column 2
pinMode (8, INPUT);//Column 3
digitalWrite(4, HIGH);//Pull up resistor of Arduino
digitalWrite(6, HIGH);// Pull up resistor of Arduino
digitalWrite(8, HIGH);// Pull up resistor of Arduino

   Serial.begin(9600);
   Serial.println("Testing...."); 
   mySerial.begin(9600);
   mySerial.println("Successful!");
    pinMode(ledPin, OUTPUT);
    pinMode(led, OUTPUT);
    digitalWrite(ledPin, LOW);
    ltr.begin( makeKeymap(alpha2Keys) );
    letters.begin( makeKeymap(alpha3Keys) );
    ltrpad.begin( makeKeymap(alphaKeys) );
    numpad.begin( makeKeymap(numberKeys) );
    letters.addEventListener(keypadEvent_ltr3);
    ltr.addEventListener(keypadEvent_ltr2); 
    ltrpad.addEventListener(keypadEvent_ltr);  
    letters.setHoldTime(300);  
    ltr.setHoldTime(300);   
    ltrpad.setHoldTime(300);                   
    numpad.addEventListener(keypadEvent_num);  
    numpad.setHoldTime(300);                   
}

char key;

void loop() {

    if( alpha )
        key = ltrpad.getKey( );
    else if( alpha2 )
        key = ltr.getKey( );
    else if( alpha3 )
        key = letters.getKey( );
    else
        key = numpad.getKey( );

    if (alpha && millis()-startTime>100) {  
      digitalWrite(ledPin,!digitalRead(ledPin));
        startTime = millis();
    }
    else if (alpha2 && millis()-startTime>100) {  
      digitalWrite(ledPin,!digitalRead(ledPin));
        startTime = millis();
    }
    else if (alpha3 && millis()-startTime>100) {  
      digitalWrite(ledPin,!digitalRead(ledPin));
        startTime = millis();
    }
    if (key != NO_KEY)
{
digitsEntered++;
mySerial.begin(9600);
mySerial.print(key);
delay(100);
}
}

static char virtKey = NO_KEY;   
static char physKey = NO_KEY;
static char buildStr[12];
static byte buildCount;
static byte pressCount;

static byte kpadState;


void keypadEvent_ltr(KeypadEvent key) {
    kpadState = ltrpad.getState( );
    swOnState( key );
}
void keypadEvent_ltr2(KeypadEvent key) {
    kpadState = ltr.getState( );
    swOnState( key );
} 
void keypadEvent_num( KeypadEvent key ) {
    kpadState = numpad.getState( );
    swOnState( key );
}

void keypadEvent_ltr3(KeypadEvent key) {
    kpadState = letters.getState( );
    swOnState( key );
} 

void swOnState( char key ) {
    switch( kpadState ) {
        case PRESSED:
            if (isalpha(key)) {             
                if (physKey != key) {        
                    pressCount = 0;
                    virtKey = key;
                    physKey = key;
                }
                else {                       
                    virtKey++;                   
                    pressCount++;                
                }
                    if (pressCount > 2) {    
                        pressCount = 0;
                        virtKey = key;
                    }
                    Serial.print(virtKey);   
                }

                if (isdigit(key) || key == ' ' || key == '.')
                    Serial.print(key);
                if (key == '#')
                    Serial.println();

        case HOLD:
            if (key == '#')  {               
                if (alpha == true)  {        
                    alpha = false;           
                    digitalWrite(ledPin, LOW);
                    digitalWrite(led, LOW);
                }
                else  {                      
                    alpha = true;            
                }

            }
            else if (key == '*')  {               
                if (alpha2 == true)  {        
                    alpha2 = false;           
                    digitalWrite(ledPin, HIGH);           
                    digitalWrite(led, HIGH);

                }
                else  {                      
                    alpha2 = true;            
                }

            }
            else if (key == '*')  {               
                if (alpha3 == true)  {        
                    alpha3 = false;           
                    digitalWrite(ledPin, HIGH);           
                    digitalWrite(led, HIGH);

                }
                else  {                      
                    alpha3 = true;            
                }

            }   

            else  {                          
                buildStr[buildCount++] = (isalpha(key)) ? virtKey : key;
                buildStr[buildCount] = '\0';
                Serial.println();
                Serial.println(buildStr);
            }
            break;

        case RELEASED:
            if (buildCount >= sizeof(buildStr))  buildCount = 0;  
            break;

    }  
}

还有一种方法可以通过电话将arduino识别为键盘吗?

4

0 回答 0