Go Down

Topic: Need help with reading keyboard into an array (Read 1 time) previous topic - next topic

doulos24

Feb 03, 2013, 09:14 pm Last Edit: Feb 04, 2013, 02:15 am by doulos24 Reason: 1
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: [Select]
// 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 {
 
  }
 }  
}




HazardsMind

Your missing an opening bracket at your first IF statement.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

doulos24

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

HazardsMind

Usually it's  'PS2_ENTER', with the single quotes ' '. We're you given an example with that ps2 library?
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

doulos24

#4
Feb 04, 2013, 04:22 am Last Edit: Feb 04, 2013, 04:31 am by doulos24 Reason: 1
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?
*/

HazardsMind

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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

holmes4

It should be PS2_ENTER and not 'PS2_ENTER`.

Mark

GoForSmoke


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: [Select]

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?

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

doulos24

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  

 

michinyon

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

HazardsMind

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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

GoForSmoke


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.

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

doulos24

#12
Feb 04, 2013, 02:04 pm Last Edit: Feb 04, 2013, 02:21 pm by doulos24 Reason: 1
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.



   

HazardsMind

#13
Feb 04, 2013, 02:09 pm Last Edit: Feb 04, 2013, 02:34 pm by HazardsMind Reason: 1
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: [Select]
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
       }
   }
}
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

doulos24

#14
Feb 04, 2013, 02:31 pm Last Edit: Feb 04, 2013, 02:33 pm by doulos24 Reason: 1
I'll try that thanks for some help

Go Up