Pages: [1] 2   Go Down
Author Topic: Need help with reading keyboard into an array  (Read 1093 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

here is my code
 
I simply need to take "char c = keyboard.read();" and make it into an array reading the bytes from the keyboard and sending the array across the serial port when I press enter. I need to do this to avoid the serial time out. I will send a string like this 12.900.14.14 then when I press enter it sends it as one string across the serial port.

please help me finish this project

thanks
doulos


Code:
// arduino serial terminal  

#include <PS2Keyboard.h>
#include "Wire.h"
#include "LiquidCrystal.h"

LiquidCrystal lcd(0);
int reset_pin = 13;
const int DataPin = 3;
const int IRQpin =  2;

PS2Keyboard keyboard;

void setup() {
  pinMode(reset_pin, OUTPUT);
  digitalWrite(reset_pin, LOW);
  delay(1000);
  keyboard.begin(DataPin, IRQpin);
  Serial.begin(9600);
  delay(1000);
  lcd.begin(16, 2);
  lcd.setBacklight(HIGH);
  lcd.setCursor(0, 0);
  lcd.print(":");
}

void loop() {
    
    
    
    
if (keyboard.available(){
  char c = keyboard.read();  // I need to make char c an array  
   lcd.print (c); // and i also need to print char c= keyboard.read();  one char at a time on the lcd
  
if (c == PS2_ENTER) {
      
     //then send the array accross the serial port here to avoid serial timeout
    
     Serial.println();
       delay(20);
       lcd.clear();  
       delay(10);
       lcd.print(":");    

    } else if (c == PS2_DELETE) { // this is using pin 13 to reset another arduino  
      lcd.print("reset");
      digitalWrite(reset_pin , LOW);
      delay(2000);
      digitalWrite(reset_pin , HIGH);  
      lcd.clear();
    } else {
  
   }
  }  
 }



« Last Edit: February 03, 2013, 08:15:27 pm by doulos24 » Logged

Queens, New York
Online Online
Faraday Member
**
Karma: 67
Posts: 2944
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your missing an opening bracket at your first IF statement.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for that, but that doesn't actually solve anything 
Logged

Queens, New York
Online Online
Faraday Member
**
Karma: 67
Posts: 2944
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Usually it's  'PS2_ENTER', with the single quotes ' '. We're you given an example with that ps2 library?
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes I was and that part of the code works the whole thing works as it reads single chars and prints them, but I need to store keyboard.read(); as an Array and send it in 1 chunk when it sees 'PS2_ENTER' and I don't know how to do that. If I just stream my input the Serial port pings and I have 4 seconds to enter my string. I even know the max buffer I'll use as it is a 16 char display so char line [17]

I understand the concept I just don't know how to impliment it.

I need to have  storage for the max array   something like

char line [17] ; // max array length above void setup
int count = 0 ; // a counter  above void setup

then something like

//in the void loop

/*

void loop() {

if keyboard.available () > 0 {
line[count] == (char)keyboard.read();
if (line[count++] == 'PS2_ENTER'  
line[count] = '\0'; // zero-terminate  
Serial.print(line); // print array
delay(20);
count =0; // reset counter
 }
}


//but this doesn't work so can I get some help?
 */
« Last Edit: February 03, 2013, 10:31:40 pm by doulos24 » Logged

Queens, New York
Online Online
Faraday Member
**
Karma: 67
Posts: 2944
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok then think of it like this. You want to enter an array of chars and send it when you press enter, so if the incoming chars are NOT "!=" EQUAL to "enter", store it, else print / write it.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 25
Posts: 1881
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It should be PS2_ENTER and not 'PS2_ENTER`.

Mark
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4036
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes I was and that part of the code works the whole thing works as it reads single chars and prints them, but I need to store keyboard.read(); as an Array and send it in 1 chunk when it sees 'PS2_ENTER' and I don't know how to do that. If I just stream my input the Serial port pings and I have 4 seconds to enter my string. I even know the max buffer I'll use as it is a 16 char display so char line [17]



I understand the concept I just don't know how to impliment it. 


Code:
char line [17] ; // max array length above void setup
int count = 0 ; // a counter  above void setup

//then something like

//in the void loop

/*

void loop() {

if keyboard.available () > 0 {
line[count] == (char)keyboard.read();
if (line[count++] == 'PS2_ENTER'  
line[count] = '\0'; // zero-terminate  
Serial.print(line); // print array
delay(20);
count =0; // reset counter
 }
}


//but this doesn't work so can I get some help?
 */

I can read my PC keyboard through serial monitor while in the IDE, or hyperterminal which I can copy content from. On Arduino I read that through Serial as in Serial.begin(), Serial.available() and Serial.Read. Some terminals send keys as hit and some send whole lines when enter is hit.

What keyboard you got? What Arduino are you using?

Logged

Examples can be found in your IDE.

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I dont think the serial terminal is important the point is I'm making my own terminal this is not connected to a computer at all the concept is this using two arduinos 1 freeduino  and 1 arduino mega.

read the PS2 keyboard.read(); as an array // on the freeduino
read the keyboard.read(); input to a lcd screen as well // on the freeduino
when PS2_ENTER is read
send the array to the mega via hardware serial pins  0 and 1
or even software pins 10 and 11  
or even i2c

whatever works

read the serial stream on the mega

say 12.900.12.12

parse the input
and run the code on the  mega  

in a perfect world I'd like to get back the mega data on the uno lcd

but I can deal with 2 lcd screens using i2c no big deal there  

  
Logged

Offline Offline
Edison Member
*
Karma: 29
Posts: 2047
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to learn when to use one =  and when to use two ==
Logged

Queens, New York
Online Online
Faraday Member
**
Karma: 67
Posts: 2944
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok then, lets start from what you are able to get. BTW, your lcd address should not be just 0, it should be something like 0x20 or 0x??, otherwise it won't show anything.

Bed now, see you in 8 hours.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4036
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I dont think the serial terminal is important the point is I'm making my own terminal this is not connected to a computer at all the concept is this using two arduinos 1 freeduino  and 1 arduino mega.

read the PS2 keyboard.read(); as an array // on the freeduino
read the keyboard.read(); input to a lcd screen as well // on the freeduino
when PS2_ENTER is read
send the array to the mega via hardware serial pins  0 and 1
or even software pins 10 and 11  
or even i2c

whatever works

read the serial stream on the mega

say 12.900.12.12

parse the input
and run the code on the  mega  

in a perfect world I'd like to get back the mega data on the uno lcd

but I can deal with 2 lcd screens using i2c no big deal there  


Do you think that you could convert code that processes commands on Serial to use your devices instead? There's free examples of things like that for Serial.

Logged

Examples can be found in your IDE.

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Ok then, lets start from what you are able to get. BTW, your lcd address should not be just 0, it should be something like 0x20 or 0x??, otherwise it won't show anything.

Bed now, see you in 8 hours.


your somehow asuming this code doesn't work it does  
this is just I2C address stating this is the first lcd screen on the i2c
it works fine. i.e. // Connect via i2c, default address #0 (A0-A2 not jumpered)

from Adafruits liquid crystal library  

Quote
Do you think that you could convert code that processes commands on Serial to use your devices instead? There's free examples of things like that for Serial.


no sorry it needs to be stand alone all this crap is controlling another pc already that is running code and autohotkey scripts. The purpose of the terminal is to eliminate having to enter a serial terminal to send this which already works to avoid hardware timeouts on another microprocessor device.



    
« Last Edit: February 04, 2013, 08:21:51 am by doulos24 » Logged

Queens, New York
Online Online
Faraday Member
**
Karma: 67
Posts: 2944
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So when you press a key, your able to see its value on the lcd? Ok, so then store it in an array, and keep storing the chars until enter is pressed.

Code:
void loop() {

if (keyboard.available () > 0) {
    KB_chars = keyboard.read(); // new variable "KB_chars", instead of "c".  Better name.
    if ( KB_chars != PS2_ENTER){ //If the key(s) pressed is not PS2_ENTER, then store chars in array
       line[count++] = KB_chars;
       Serial.print(line[count]); // show chars being entered.
       }
    else {  // If PS2_ENTER is pressed, print them.
       line[count++] = '\0'; // once enter is pressed, count +1 then add zero-terminate 
       Serial.print(line); // Not sure if it will work like this, might need to convert it to a string first. I'll double check. print array
       //delay(20);
       count = 0; // reset counter
       }
   }
}
 
« Last Edit: February 04, 2013, 08:34:37 am by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'll try that thanks for some help
« Last Edit: February 04, 2013, 08:33:35 am by doulos24 » Logged

Pages: [1] 2   Go Up
Jump to: