Arduino as interface for PC control

Hi, I have an Arduino controlling the lighting and a few power sockets around my desk using a modified version of the code below. What I am after, alongside the buttons currently used in this program, is a way to trigger these "switches" with a PC.

int buttonPin1  =2; // the number of the pushbutton pin
int buttonPin2  =3;
int relayPin1  =6; // the number of the relay pin
int relayPin2  =7; 
int poweron  =12;

// Variables will change:

bool relayState1 = LOW; // the current state of the output pin
bool relayState2 = LOW;

void setup() {

pinMode(buttonPin1,INPUT);
pinMode(buttonPin2,INPUT);
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
pinMode(poweron, OUTPUT);
// set initial LED state

digitalWrite(relayPin1, relayState1);
digitalWrite(relayPin2, relayState2);
digitalWrite(poweron, HIGH);
delay(3000);
digitalWrite(poweron, LOW);
}

int count1 =0;
int count2 =0;

void loop() {

// read the state of the switch into a local variable:
int reading1 = digitalRead(buttonPin1);
int reading2 = digitalRead(buttonPin2);

if (reading1 == HIGH)
  {count1++;
   if (count1==5) relayState1= !relayState1;
  } else
  count1=0;

if (reading2 == HIGH)
  {count2++;
   if (count2==5) relayState2= !relayState2;
  } else
  count2=0;


digitalWrite(relayPin1, relayState1);
digitalWrite(relayPin2, relayState2);
delay (10);
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
}

Would there be a way to use an arduino (Leonardo with its ATmega32u4, maybe?) as an interface from the PC, preferably operated by keyboard shortcut or batchfile (I have spare "G keys" on my Logitech Orion). Even if the interface arduino was used to turn a signal from the PC to a relay toggling for a second (to represent a button push), and then physically hardwired to the existing arduino.

Thanks for any help/directions pointed, even if I'm currently barking up the wrong tree!

Hi,
Look at: "Firmata" See Reference HERE

In Arduino IDE, Go to File>Examples>Firmata

You have a few steps here.

  1. catch the keypress on your PC,
  2. communication between PC and Arduino,
  3. switching the relay based on the received commands.

I take it 1) and 3) you got under control; how do you pan to do 2)?

Sending data from a PC to an Arduino should be straightforward. Have a look at this Python - Arduino demo and at Serial Input Basics.

You can use any PC programming language to do the same thing if you don’t want to use Python.

…R

wvmarle:
I take it 1) and 3) you got under control; how do you pan to do 2)?

You are correct, this was the main issue - 1) is also somewhat affect by how I perform number 2.

Thanks for the help, I'll definitely have a read through the links!