Go Down

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

doulos24

#20
Feb 04, 2013, 05:10 pm Last Edit: Feb 04, 2013, 10:16 pm by doulos24 Reason: 1
here is the working code. Only thing I'd like to be able to also read  PS2_DELETE
but I couldn't get that working but this works for now.

Code: [Select]
// arduino serial terminal  

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

LiquidCrystal lcd(0); // set lcd i2c adress
int reset_pin = 13;  // reset pin on the arduino mega
const int DataPin = 3; // data pin for ps/2
const int IRQpin =  2; // clock pn for ps/2
char line[16]; // store char array
int count = 0; // counter
unsigned long time = 60000; // 1 minuite delay

PS2Keyboard keyboard;

void setup() {

 pinMode(reset_pin, OUTPUT); // set pin 13 to output
 digitalWrite(reset_pin, HIGH);  // set pin High
 delay(1000); // delay 1 second
 keyboard.begin(DataPin, IRQpin); // start keyboard
 Serial.begin(9600); // start serial
 delay(1000);  // delay 1 second
 lcd.begin(16, 2);  // begin lcd
 lcd.setBacklight(HIGH); // turn backlight on
 lcd.setCursor(0, 0); // set cursor
 lcd.print(":"); // print a prompt
}

void loop() {

 if (keyboard.available() ){
   char kb = keyboard.read();  // read the ps2 keyboard input


   if(kb != PS2_ENTER){ // if input does not equal enter
     lcd.print(kb); // print char to lcd
     delay(20); // delay 20 ms
     line[count++] = kb; // add chars
     delay(50);  // delay 50 ms


   }
   else{
     line[count++] ='\0';  // line = line + count = terminate
     Serial.print(line);  // print the added chars
     delay(1000);  // dealy 1 second
     count = 0;  // reset count
     lcd.clear();  // clear lcd
     delay(50);  // delay 50ms
     lcd.print ("sending"); // print sending to lcd
     delay(5000); // delay 5 seconds
     lcd.clear();  // clear lcd
     delay(50); // delay 50ms
     lcd.print(":"); // print prompt  
     delay(time); // delay 1 minuite
     lcd.print("reset");  // print reset to lcd
     delay(1000); // delay 1 second
      digitalWrite(reset_pin , LOW); // send a reset to the arduino mega
     delay(2000); // delay 2 seconds
     digitalWrite(reset_pin , HIGH);  // finish reset  
     lcd.clear();     // clear lcd
 
   }
 }
}

doulos24

#21
Feb 04, 2013, 05:25 pm Last Edit: Feb 04, 2013, 05:28 pm by doulos24 Reason: 1
And here is the code to go with it  

Code: [Select]
/* max 24 relay controller and pump pulser simulator  

this program takes a serial string like this "12.900.14.14"  and parses it up into variables
for relay count 1-12, number of pulses, pulse width, and delay beween pulses.
Then it prints that to the serial port like this "Relays 12 Pulses 900 Length 14ms Delay 14ms"

created 1/7/13
last edit 2/1/13
by Will Meyers  

*/

unsigned long counter =0; // set counter to 0
int pulse =52; // pin thats connected to the pulse simulator
int led = 13; // status led
int dly= 150; // delay between relay functions

int relay[]={
 14, 15, 16, 17, 18, 19, 20, 21, 30, 31,\
32, 33, 34, 35, 36, 36, 38, 39, 40, 41, 42, 43, 44, 45}; // relay array with all 24 pins

int pinCount = 24;  // the number  index for the array



void setup() // setup arduino functions
{

 pinMode (pulse, OUTPUT);  // set pin 52 to an output
 pinMode (led, OUTPUT); // set pin 13 to an output

 Serial.begin(9600);    // start serial connection                                                                                                                                                                                                              
 Serial.println("reset");

 for(int n=0; n<pinCount;n++){  // for loop
   pinMode(relay[n],OUTPUT);  // set all relay pins as output
   digitalWrite(relay[n], HIGH); // write all relay pins high SETS THEM TO OFF
 }
}

void loop()  // run main loop
{
 if (Serial.available() > 0)  // wait for data
 {

   int w = Serial.parseInt();   // this is the relay selection variable from 1 -12
   unsigned long x = Serial.parseInt();  // this is for the pulse counter
   int y = Serial.parseInt(); // this sets how long you want to hold the pulse high  
   int z = Serial.parseInt(); // delay between pulses
   delay(1); // neccesary delay to use Serial.parseint() ;
   Serial.print("relays "); // print "relays " to serial port
   Serial.print (w * 2);   // print the w x 2 or relay pins to the serial port
   Serial.print(" Pulses "); // print "pulses "to the serial port
   Serial.print(x); // print the var value of x to the serial port
   Serial.print(" Length "); // print "length " to the serial port
   Serial.print(y);
   Serial.print( "ms"); // print the value of y then "ms"  to the serial port
   Serial.print(" delay " );  // print "delay"  to the serial port
   Serial.print(z); // print the value of z  to the serial port
   Serial.println("ms");  //print "ms" to the serial port

   for(int n=0; n < (w * 2); n++){  // for loop relay counter * 2
     digitalWrite(relay[n], LOW); // set all selected pins low  or ON
     delay(dly); // delay for variable
   }
   delay(dly * w );  // max delay needed  to turn on all pumps the var dly * relay selection 1-12  

   while(counter<(x)) // while counter is less the x do this loop
   {
     PORTB = 0b10000010; // port adress of the pulse pin 52 pull it HIGH
     delay(y) ;       // delay by y  or how long to hold the pin high
     PORTB = 0b00000000; // pull everything on this port LOW  
     delay(z);   // delay by z delay between pulses
     counter++; // increment counter by 1

     while(counter==(x)) // after counter equals x do this loop
     {
       delay(10); // delay by the value of 10ms
       PORTB = 0b00000000; // set all pins in this port LOW
       delay(1000); // delay by the value of 1 second

       for(int n=0; n < (w * 2); n++){ // for loop  relay counter * 2
         digitalWrite(relay[n], HIGH);// write n # of relay pins HIGH or OFF
         delay(dly); // delay by variable dly

       }
       Serial.println("done");  // print "done" to the serial port on a new line
       return; // fin
     }
   }
 }
}


doulos24


Go Up