How to send keyboard characters over serial port?

Hi,

I have project where I will build a test equipment with two arduino boards(2009) talking via bluetooth http://www.sparkfun.com/products/10559. One “measure” unit and one “remote” unit.
The measure unit will use 2 analog inputs to measure pneumatic flow and preassure in a machine, 1 digital input from the flowmeter as an alarm if the flow is to high and 1 digital output to shut on and off the valve.
The remote unit has a keypad and a LCD 8*2.
I will also later on use a Micro SD shield http://www.sparkfun.com/products/9802 to store the readed values later on, but I have not decided yet on wich unit I will put this.

I have come this far: Reading analog inputs when pushing a button is working, and sending the values over BT to the other one is also ok. What is not working is to trig the reading via serial communication from the remote unit to the measure unit. For example, I would like to trig the flow and preassure reading when the * is pressed down on my keypad. I found some code before that should send which key got pressed on the keypad, but I found out that the measure unit was trigged of anything that came in at the RX port, so I erased it. When this starts to work will I use more buttons to other functions, that´s why I have a complete keypad.

Can someone help me to get further with the serial communication before I get crazy :~. I have tried to find something useful at the forum but without success.
Could I use a For instruction to get a nicer code where I read the values?
Thank´s in advance!!

//Per

This is the code for the measure unit:

int FlowmeterPin = 3;      // potentiometer wiper (middle terminal) connected to analog pin 0
int PressurePin = 1;       // Pressuresensor connected to analog pin 1
int TestbuttonPin = 2;     // Pushbutton to trig analog reading and evaluate a average value
int TestbuttonPin_2 = 5;   // Pushbutton to trig analog reading and evaluate a average value
//int FlowalarmPin = 13;   // Input pin from flowmeter if flow exceeds 95 liter/min
int TestvalvePin = 4;      // Output pin to activate flowmeter valve (normal= Low, test= High)
int LEDPin = 3;

int incomingByte = 0;	   // for incoming serial data

int Flow_1 = 0;            // variable to store the value read 1
int Flow_2 = 0;            // variable to store the value read 2
int Flow_3 = 0;            // variable to store the value read 3
int Flow_4 = 0;            // variable to store the value read 4
int Flow_Sum = 0;          // variable to store the value read

int Pressure_1 = 0;        // variable to store the value read 1
int Pressure_2 = 0;        // variable to store the value read 2
int Pressure_3 = 0;        // variable to store the value read 3
int Pressure_4 = 0;        // variable to store the value read 4
int Pressure_Sum = 0;      // variable to store the value read


void setup()
{
  Serial.begin(9600);      //  setup serial
}

void loop()
{
if (digitalRead(TestbuttonPin) == HIGH )
{
  digitalWrite (LEDPin, HIGH);             // Shows that the arduino is reading the inputs
 
  Flow_1 = analogRead(FlowmeterPin);       // read the input pin
  Pressure_1 = analogRead(PressurePin);    // read the input pin
  delay (100);
  Flow_2 = analogRead(FlowmeterPin);       // read the input pin
  Pressure_2 = analogRead(PressurePin);    // read the input pin
  delay (100);
  Flow_3 = analogRead(FlowmeterPin);       // read the input pin
  Pressure_3 = analogRead(PressurePin);    // read the input pin
  delay (100);
  Flow_4 = analogRead(FlowmeterPin);       // read the input pin
  Pressure_4 = analogRead(PressurePin);    // read the input pin
  delay (100);
  
  Flow_Sum = (Flow_1 + Flow_2 + Flow_3 + Flow_4)/4;                        //calculates the average value of the four values
  Pressure_Sum = (Pressure_1 + Pressure_2 + Pressure_3 + Pressure_4) /4;   //calculates the average value of the four values
  Serial.print(Flow_Sum, DEC);                                             //debug value
    Serial.print(",");                                                     //debug value
      Serial.print(Pressure_Sum, DEC);                                     //debug value
  digitalWrite (LEDPin, LOW);
}
}

This is the code for the remote unit:

// include the library code:
#include <LiquidCrystal.h>
#include <Keypad.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);


//For use of the Keyboard

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 17, 14, 15, 16 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 10, 8, 12 }; 

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
    // set up the LCD's number of columns and rows:
  lcd.begin(8, 2);
  // initialize the serial communications:
  Serial.begin(9600);
}

void loop()
{
  
//For the Keyboard

 char key = kpd.getKey();
  if(key)  // same as if(key != NO_KEY)
  {
    switch (key)
    {
      case '*':
//        lcd.print("*");
       Serial.print('Read');
        break;
      case '#':
 //       digitalWrite(ledpin, HIGH);
//        lcd.print("#");
        break;
      default:
        lcd.print("key");
  }
  }
   // when characters arrive over the serial port...
  if (Serial.available()) {
    // wait a bit for the entire message to arrive
    delay(200);
    // clear the screen
    lcd.clear();
    // read all the available characters
    while (Serial.available() > 0) {
      // display each character to the LCD
      lcd.write(Serial.read());
//      lcd.blink();
    }
  }
}

IMG_1460.jpg

       Serial.print('Read');

Some research on the difference between " and ’ is in your (immediate) future. Hint: ’ goes with A character while " goes with a collection of characters.

   // when characters arrive over the serial port...
  if (Serial.available()) {
    // wait a bit for the entire message to arrive
    delay(200);

To ensure that the whole message has arrived, you need to wait longer than that. A couple of hours ought to do. Or, you can get smarter about it, and read data until some value arrives that defines the end of the message/packet stream.

    while (Serial.available() > 0) {
      // display each character to the LCD
      lcd.write(Serial.read());

Some research on the difference between the write() and the print() functions would be useful, too. They are not always interchangeable. Using write() with an LCD is rarely the right thing to do.

  Flow_1 = analogRead(FlowmeterPin);       // read the input pin
  Pressure_1 = analogRead(PressurePin);    // read the input pin
  delay (100);
  Flow_2 = analogRead(FlowmeterPin);       // read the input pin
  Pressure_2 = analogRead(PressurePin);    // read the input pin
  delay (100);
  Flow_3 = analogRead(FlowmeterPin);       // read the input pin
  Pressure_3 = analogRead(PressurePin);    // read the input pin
  delay (100);
  Flow_4 = analogRead(FlowmeterPin);       // read the input pin
  Pressure_4 = analogRead(PressurePin);    // read the input pin
  delay (100);

Something wrong with using a loop?

Flow = 0;
Pressure = 0;
for(byte i=0; i<4; i++)
{
   Flow += analogRead(FlowmeterPin);
   Pressure += analogRead(PressurePin);
}

Saving the actual readings is a waste of memory, since you never use them.

Somewhere on the measure unit, it might be smart to read what the remote unit wrote to the serial port, wouldn’t it? How else is the measure unit to know that the remote unit wants it to take a reading?