Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 带Arduino的字母数字电话键盘_C++_Arduino_Keypad - Fatal编程技术网

C++ 带Arduino的字母数字电话键盘

C++ 带Arduino的字母数字电话键盘,c++,arduino,keypad,C++,Arduino,Keypad,我试图制作一个电容式触摸键盘,从用户那里获取输入,并输出由该输入生成的字符串。每个焊盘返回其值,第一个为1,第二个为2,第三个为4、8、16,直到2^12为止。(如果触摸了多个键,它们的ID将相加,但不会发生这种情况。) 假设您正在使用旧诺基亚手机,向朋友键入信息,按2两次表示“b”,按9三次表示“y”。我需要这样的代码 我最大的障碍是我对C语言的了解非常有限,到目前为止我所做的一切都很顺利,偶尔会打嗝,但这远远超出了我的知识范围 这是我目前的代码,它并不漂亮,但这是我目前的尝试 //impor

我试图制作一个电容式触摸键盘,从用户那里获取输入,并输出由该输入生成的字符串。每个焊盘返回其值,第一个为1,第二个为2,第三个为4、8、16,直到2^12为止。(如果触摸了多个键,它们的ID将相加,但不会发生这种情况。)

假设您正在使用旧诺基亚手机,向朋友键入信息,按2两次表示“b”,按9三次表示“y”。我需要这样的代码

我最大的障碍是我对C语言的了解非常有限,到目前为止我所做的一切都很顺利,偶尔会打嗝,但这远远超出了我的知识范围

这是我目前的代码,它并不漂亮,但这是我目前的尝试

//import all packages
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1351.h>
#include <Adafruit_MPR121.h>
#include <Adafruit_NeoPixel.h>
#include <SPI.h>
//define pins for OLED display
#define sclk SCK
#define mosi MOSI
#define dc   A1
#define cs   A2
#define rst  A3
//define pins for indicator lights
#define neo_pin A4
//define basic colours
#define BLACK           0x0000
#define BLUE            0x001F
#define RED             0xF800
#define GREEN           0x07E0
#define CYAN            0x07FF
#define MAGENTA         0xF81F
#define YELLOW          0xFFE0 
#define WHITE           0xFFFF
//create the screen
Adafruit_SSD1351 screen = Adafruit_SSD1351(cs, dc, mosi, sclk, rst);
//create the cap. sensor
Adafruit_MPR121 cap = Adafruit_MPR121();
//initialize the variables used by the cap. sensor
uint16_t lastTouched = 0;
uint16_t currTouched = 0;
int delayPress = -1;
int buttonIndex = 0;
int padTouched = 0;
//create the neopixel indicator light
Adafruit_NeoPixel indicator = Adafruit_NeoPixel(1, neo_pin, NEO_GRB + NEO_KHZ800);
//initialize functional variables
char string[32];
//level 0 functions
void flash(int dly = 100)
{
  digitalWrite(13,HIGH);
  delay(dly);
  digitalWrite(13,LOW);
  delay(dly);
}
void flashIndicator(int dly = 100)
{
  setColour(255,255,255);
  delay(dly);
  setColour(0,0,0);
  delay(dly);
}
void setColour(int r, int g, int b)
{
  indicator.setPixelColor(0,r,g,b);
  indicator.show();
}
void resetDrawText(char *text,uint16_t textColour = WHITE, uint16_t backColour = BLACK)
{
  screen.fillScreen(backColour);
  screen.setCursor(0,0);
  screen.setTextColor(textColour);
  screen.print(text);
}
void drawText(char *text,uint16_t textColour = WHITE)
{
  screen.setCursor(0,0);
  screen.setTextColor(textColour);
  screen.print(text);
}

//component startup functions (level 1)
void startIndicator()
{
  indicator.begin();
  indicator.setBrightness(64);
  setColour(0,0,0);
  setColour(0,255,0);
}
void startCap()
{
  cap.begin(0x5A);
}
void startScreen()
{
  screen.begin();
  screen.fillScreen(BLACK);
}

void setup() {
  // start all components
  startIndicator();
  startCap();
  startScreen(); 
  Serial.begin(9600);
  Serial.println("init");
  setColour(255,0,255);
}

void keyIn()
{
  flashIndicator(100);
  flashIndicator(100);

  char catChar[] = " ";
  Serial.println("pad index");
  Serial.println(currTouched);
  switch (currTouched)
  {
    case 1:
    catChar[1] = "A";
    break;

    case 2:
    catChar[1] = "B";
    break;

    case 4:
    catChar[1] = "C";
    break;
  }
  //Serial.println("char group");
  //Serial.println(catGroup);


  //strcpy(catChar, catGroup);
  //char catChar = catGroup[buttonIndex];
  Serial.println("new char");
  Serial.println(catChar);

  strcat(string, catChar);
  drawText(string);
  Serial.println("draw text");
  Serial.println(string);
  delayPress = -1;
  buttonIndex = 0;
  padTouched = 0;
}

void loop() {
  currTouched = cap.touched();

  if ((currTouched > 0) and (not currTouched == lastTouched))
  {
    Serial.println("pad touched");
    Serial.println(currTouched);
    flashIndicator(200);
    if ((padTouched == currTouched) or (padTouched == 0))
    {
    delayPress = 50;

    buttonIndex++;
    if (buttonIndex > 2)
    {
        buttonIndex = 0;
    }
    Serial.println("button index");
    Serial.println(buttonIndex);
    }
    else if (not currTouched == 0)
    {
    keyIn();
    }
    padTouched = currTouched;
  }
  if (delayPress > 0)
  {
    delayPress--;
  }
  else if (delayPress == 0)
  { 
    keyIn();
  }
  lastTouched = currTouched;

  flash(50);//delay(100);
}
//导入所有包
#包括
#包括
#包括
#包括
#包括
//定义OLED显示器的引脚
#定义sclk SCK
#定义mosi mosi
#定义dc A1
#定义cs A2
#定义rst A3
//定义指示灯的引脚
#定义neo_引脚A4
//定义基本颜色
#定义黑色0x0000
#定义蓝色0x001F
#定义红色0xF800
#定义绿色0x07E0
#定义青色0x07FF
#定义品红0xF81F
#定义黄色0xFFE0
#定义白色0xFFFF
//创建屏幕
Adafruit_SSD1351屏幕=Adafruit_SSD1351(cs、dc、mosi、sclk、rst);
//创建封口。传感器
Adafruit_MPR121帽=Adafruit_MPR121();
//初始化cap使用的变量。传感器
uint16_t lastTouched=0;
uint16\u t=0;
int delayPress=-1;
int按钮索引=0;
int=0;
//创建neopixel指示灯
Adafruit_NeoPixel指示器=Adafruit_NeoPixel(1,neo_引脚,neo_GRB+neo_KHZ800);
//初始化函数变量
字符串[32];
//0级功能
无效闪烁(int dly=100)
{
数字写入(13,高);
延迟(dly);
数字写入(13,低);
延迟(dly);
}
真空闪蒸指示器(int dly=100)
{
SetColor(255255);
延迟(dly);
SetColor(0,0,0);
延迟(dly);
}
无效设置颜色(整数r、整数g、整数b)
{
指示剂。设置像素颜色(0,r,g,b);
指示符show();
}
void resetDrawText(字符*文本,uint16\u t文本颜色=白色,uint16\u t背景颜色=黑色)
{
屏幕。填充屏幕(背景色);
屏幕设置光标(0,0);
screen.setTextColor(textcolor);
屏幕打印(文本);
}
无效drawText(字符*文本,uint16\u t文本颜色=白色)
{
屏幕设置光标(0,0);
screen.setTextColor(textcolor);
屏幕打印(文本);
}
//组件启动功能(1级)
void startIndicator()
{
指示器。开始();
指标.正确性(64);
SetColor(0,0,0);
setcolor(0255,0);
}
void startCap()
{
盖开始(0x5A);
}
void startScreen()
{
screen.begin();
屏幕。填充屏幕(黑色);
}
无效设置(){
//启动所有组件
startIndicator();
startCap();
startScreen();
Serial.begin(9600);
Serial.println(“init”);
SetColor(255,0255);
}
void keyIn()
{
闪光指示器(100);
闪光指示器(100);
char catChar[]=“”;
Serial.println(“pad索引”);
Serial.println(curr);
开关(接触式)
{
案例1:
catChar[1]=“A”;
打破
案例2:
catChar[1]=“B”;
打破
案例4:
catChar[1]=“C”;
打破
}
//Serial.println(“字符组”);
//串行打印LN(catGroup);
//strcpy(catChar,catGroup);
//char catChar=catGroup[按钮索引];
Serial.println(“新字符”);
连载println(catChar);
strcat(字符串、catChar);
drawText(字符串);
Serial.println(“绘制文本”);
Serial.println(字符串);
延迟压力=-1;
按钮指数=0;
padd=0;
}
void循环(){
currTouched=cap.toucted();
如果((currTouched>0)和(非currTouched==lastTouched))
{
串行打印(“触摸板”);
Serial.println(curr);
闪光指示器(200);
如果((padTouched==currTouched)或(padTouched==0))
{
延迟压力=50;
buttonIndex++;
如果(按钮索引>2)
{
按钮指数=0;
}
Serial.println(“按钮索引”);
Serial.println(按钮索引);
}
else if(非电流==0)
{
keyIn();
}
padpothed=currpothed;
}
如果(延迟按>0)
{
延迟按--;
}
else if(delayPress==0)
{ 
keyIn();
}
lastpothed=currpothed;
闪光(50);//延迟(100);
}

请随时询问清楚性,如果您确实知道解决方案,请解释每一行和每一句话都在做什么,以便我对语言有一些了解。

这演示了字母数字键盘键入的概念:

const int max_delay_between_keys = 300; // Time in milliseconds to allow between keypresses

String text = "";
char current_char = '\0';
int last_pressed = -1;
int times_pressed = 0;
int time_last_pressed = 0;

char lookup_value(int key, int count) {
    const char* const characters[12] = {"1", "abc2", "def3", "ghi4", "jkl5", "mno6", "pqrs7", "tuv8", "wxyz9", "*", "0 ", "#"}; //Change this to match the characters on your keypad.
    const char* sequence = characters[key];
    return sequence[count % strlen(sequence)];
}

String process_keystream(int key_code) {
    int new_time = millis();
    bool time_over = (new_time - time_last_pressed > max_delay_between_keys);

    time_last_pressed = new_time;

    key_code = log((float)key_code) / log(2.0) - 1;
    if (key_code == last_pressed && !time_over) {
        times_pressed += 1;
    }
    else {
        if (last_pressed != -1) {
            text += current_char;
        }

        last_pressed = key_code;
        times_pressed = 0;
    }
    current_char = lookup_value(key_code, times_pressed);

    return text + current_char;
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  // Serial.println() is what you'd send to your screen.
  Serial.println(process_keystream(16)); // Pressed the 4 key
  Serial.println(process_keystream(16)); // Pressed the 4 key
  Serial.println(process_keystream(8));  // Pressed the 3 key
  Serial.println(process_keystream(8));  // Pressed the 3 key
  Serial.println(process_keystream(32)); // Pressed the 5 key
  Serial.println(process_keystream(32)); // Pressed the 5 key
  Serial.println(process_keystream(32)); // Pressed the 5 key
  delay(500);                            // Wait a while so the current letter is set.
  Serial.println(process_keystream(32)); // Pressed the 5 key
  Serial.println(process_keystream(32)); // Pressed the 5 key
  Serial.println(process_keystream(32)); // Pressed the 5 key
  Serial.println(process_keystream(64)); // Pressed the 6 key
  Serial.println(process_keystream(64)); // Pressed the 6 key
  Serial.println(process_keystream(64)); // Pressed the 6 key
}

void loop() {
  // put your main code here, to run repeatedly:

}
结果:
非常感谢。只是看了一下代码,它比我的混乱xD更有意义。
g
h
hd
he
hej
hek
hel
helj
helk
hell
hellm
helln
hello