Keyboard and relay board?

Hello i am new in programming, and i have problem. I have PS/2 keyboard and relay board, and i would like to: when "Z" is pressed, the relay 4 get on. When pressed "X" the relay get off. When pressed "S" the another (3rd) relay get on. And when pressed "D" the 3rd relay get off. I have simple code here, which is based on two another codes (relay and keyboard). But it doesn't work. I don know what can i do with this errors:

Stopka.ino:66:6: error: expected constructor, destructor, or type conversion before ‘(’ token
Stopka.ino:67:9: error: expected constructor, destructor, or type conversion before ‘;’ token
Stopka.ino: In function ‘void loop()’:
Stopka.ino:79:10: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
Stopka.ino:82:15: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
Stopka.ino:85:15: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
Stopka.ino:88:15: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]

Here is my code:

//
// I bought product below from IC Station and could not immediately find example code,
// so I decided to share this.
//
// ICStation 8 Channel MICRO USB Relay Module 5V ICSE014A
// http://www.icstation.com/icstation-channel-micro-relay-module-icse014a-p-5185.html
//
// Your fellow maker,
// Hari Wiguna
// youtube.com/hwiguna

// Features:
// - Control 8 relays using only one or two GPIO pins
// - You can download a PC program to control it via USB
// - Relay contact up to 10A
// - 5V compatible, perfect to interface with Arduino

// Serial Protocol:
// At power up, module expects Arduino/PC to send 0x50
// Module would then send back an identifier: ICSE012A=0xAB, ICSE013A=0xAD, ICSE014A=0xAC
// Until this happens, module would not respond to other commands.
// To control the relays, we need to send 0x51 followed by another byte.
// the 8 bits in this byte corresponds with the 8 relays.
// WARNING: I was unable to set multiple bits to on AT ONCE. I believe this is because
// too much current is needed to do so.
// Instead, I was able to turn them one at a time within 10ms.  Inconvenient, but it works.

// Lessons learned:
// 1. DC Power jack does not work well. Maybe it's supposed to be for higher voltage?!
// 2. Relays take a lot of power, turning on multiple relays at once is unreliable.
// 3. Keep power supply for relay separate from Arduino

// Use software serial library so we won't affect the relay module while uploading sketch
#include <SoftwareSerial.h>
SoftwareSerial relay(10, 11); // RX, TX.  Connect pin 11 to the RX on the relay module.

int bitPattern = 0xFF; // all 1s means all off
int prevIndex = 0;

#include <PS2Keyboard.h>

const int DataPin = 3;
const int IRQpin =  2;

PS2Keyboard keyboard;

char c;

void setup()
{
   delay(1000);
  keyboard.begin(DataPin, IRQpin);
  delay(1000); // Open built-in RX/TX for debugging purpose.
  relay.begin(9600); // This is RX/TX for the relay modeul

  InitModule();      // Blindly initialize module, only require RX on module to be wired to TX on Arduino
  //WaitForModule(); // wait until module sends back its ID, require TX from module to be wired up to RX on Arduino
  delay(1000);
}

void InitModule()
{
  relay.write( (byte)0x50 ); // Relay module will send back acknowledgement, but we'll ignore it.
}

delay(1000);
AllOff();
// When the module is powered up, it waits for 0x50 and sends back a device identifier.
// The module would be unresponsive to commands unless this had ocurred.


void loop() // run over and over
{
  if (keyboard.available()) {
    
    // read the next key
    char c = keyboard.read();   
  }
  if (c=="z") {
    zvonekZap();
  }
  else if (c=="x") {
    zvonekVyp();
  }
  else if (c=="s") {
    stopZap();
  }
  else if (c=="d") {
    stopVyp();
  }
  else;
  //OneAtATime();
  //Blink();
  //Pot();
  //BackAndForth(1000); // Parameter is delay in miliseconds. lower = faster.
  //MultipleBits(); // This routine does NOT work
}



// Specify which relay to control (0..7)
// and whether to turn it off/on.
// Set state to 1 to turn it OFF
// Set state to 0 to turn it ON
void Switch(byte which, byte state)
{
  relay.write( (byte)0x51);
  if (state == 1) {
    bitPattern = bitSet(bitPattern, which);
  }
  else {
    bitPattern = bitClear(bitPattern, which);
  }

  Serial.println(bitPattern, BIN);
  relay.write( (byte)bitPattern);
}
void zvonekZap()
{
  relay.write( (byte)0x51);
    bitPattern = bitClear(bitPattern, 4);
  relay.write( (byte)bitPattern);
}
void zvonekVyp()
{
  relay.write( (byte)0x51);
    bitPattern = bitSet(bitPattern, 4);
  relay.write( (byte)bitPattern);
}
void stopVyp()
{
  relay.write( (byte)0x51);
    bitPattern = bitSet(bitPattern, 3);
  relay.write( (byte)bitPattern);
}
void stopZap()
{
  relay.write( (byte)0x51);
    bitPattern = bitClear(bitPattern, 3);
  relay.write( (byte)bitPattern);
}
void AllOff()
{
  for (int i = 0; i < 4; i++) {
    Switch(i, 1); // Off
    delay(5);
  }
}

PS: sorry for my English. I'm 15 years old Czech student :wink:

I have an update :slight_smile: Arduino IDE now don't writing any errors, but the program still not work? Any ideas?

//
// I bought product below from IC Station and could not immediately find example code,
// so I decided to share this.
//
// ICStation 8 Channel MICRO USB Relay Module 5V ICSE014A
// http://www.icstation.com/icstation-channel-micro-relay-module-icse014a-p-5185.html
//
// Your fellow maker,
// Hari Wiguna
// youtube.com/hwiguna

// Features:
// - Control 8 relays using only one or two GPIO pins
// - You can download a PC program to control it via USB
// - Relay contact up to 10A
// - 5V compatible, perfect to interface with Arduino

// Serial Protocol:
// At power up, module expects Arduino/PC to send 0x50
// Module would then send back an identifier: ICSE012A=0xAB, ICSE013A=0xAD, ICSE014A=0xAC
// Until this happens, module would not respond to other commands.
// To control the relays, we need to send 0x51 followed by another byte.
// the 8 bits in this byte corresponds with the 8 relays.
// WARNING: I was unable to set multiple bits to on AT ONCE. I believe this is because
// too much current is needed to do so.
// Instead, I was able to turn them one at a time within 10ms.  Inconvenient, but it works.

// Lessons learned:
// 1. DC Power jack does not work well. Maybe it's supposed to be for higher voltage?!
// 2. Relays take a lot of power, turning on multiple relays at once is unreliable.
// 3. Keep power supply for relay separate from Arduino

// Use software serial library so we won't affect the relay module while uploading sketch
#include <SoftwareSerial.h>
SoftwareSerial relay(10, 11); // RX, TX.  Connect pin 11 to the RX on the relay module.

int bitPattern = 0xFF; // all 1s means all off
int prevIndex = 0;

#include <PS2Keyboard.h>

const int DataPin = 3;
const int IRQpin =  2;

PS2Keyboard keyboard;

char c;

void setup()
{
   delay(1000);
  keyboard.begin(DataPin, IRQpin);
  delay(1000); // Open built-in RX/TX for debugging purpose.
  relay.begin(9600); // This is RX/TX for the relay modeul

  InitModule();      // Blindly initialize module, only require RX on module to be wired to TX on Arduino
  //WaitForModule(); // wait until module sends back its ID, require TX from module to be wired up to RX on Arduino
  delay(1000);
}

void InitModule()
{
  relay.write( (byte)0x50 );
 AllOff(); // Relay module will send back acknowledgement, but we'll ignore it.
}

// When the module is powered up, it waits for 0x50 and sends back a device identifier.
// The module would be unresponsive to commands unless this had ocurred.


void loop() // run over and over
{
  if (keyboard.available()) {
    
    // read the next key
    char c = keyboard.read();   
  }
  if (c == 'z') {
    Switch(4,0);
  }
  else if (c == 'x') {
    Switch(4,1);
  }
  else if (c == 's') {
    Switch(3,0);
  }
  else if (c == 'd') {
    Switch(3,1);
  }
  else;
  //OneAtATime();
  //Blink();
  //Pot();
  //BackAndForth(1000); // Parameter is delay in miliseconds. lower = faster.
  //MultipleBits(); // This routine does NOT work
}



// Specify which relay to control (0..7)
// and whether to turn it off/on.
// Set state to 1 to turn it OFF
// Set state to 0 to turn it ON
void Switch(byte which, byte state)
{
  relay.write( (byte)0x51);
  if (state == 1) {
    bitPattern = bitSet(bitPattern, which);
  }
  else {
    bitPattern = bitClear(bitPattern, which);
  }
  relay.write( (byte)bitPattern);
}
void AllOff()
{
  for (int i = 0; i < 4; i++) {
    Switch(i, 1); // Off
    delay(5);
  }
}

You're setting a local variable c to the keyboard key, once out of scope, that data is gone poof!

change this:

if (keyboard.available()) {
    
    // read the next key
    char c = keyboard.read();   
  }

to this:

if (keyboard.available()) {
    
    // read the next key
    c = keyboard.read();   
  }

Also while your at it, get rid of this else; you have at the end.

You could also potentially use a switch() statement to check your button.

Thx. Now it do something when i press one of the buttons. But it doesn't do, what i need. Now all the relays goes on, and off, and on... but very quickly. 10x per second? When i hold x, somethimes it stop, and all relays are off. When i hold d, the 2nd and 4th relays are off and 1st and 3rd rlays are on. Can you help me now?