Velleman K8056 8-channel relaycard library

Hi,

Last evening I finalized my first real library :slight_smile:
I wanted to control my Velleman K8056 relaycard with RS232 commands, using only 1 wire :stuck_out_tongue:

Feel free to comment me and advise me for better/cleaner programming:

http://www.reptile-addict.nl/arduino/?page_id=209

I wanted to put this on the playground but havn't got a clue on how to get it there :blush:

http://playground.arduino.cc//Main/Participate

You can make an entry in the Relays section.
http://playground.arduino.cc//Main/InterfacingWithHardware#Output
search for relays.
If you add [[Code/VellemanK8056]] and save it, you can go the link and open that page and fill it.
I'm not sure about the "Code" section. If you don't use the "Code" section, the page will be made in the "Main" section.

Will the library compile with Uno selected ? I'm not sure about the Serial1, Serial2, Serial3.

Why not call the function "On", instead of Relais_ON ?
If the class would be called "Relay", it would be called with Relay.Relay_ON().

You could follow the digitalWrite(pin,HIGH/LOW) function, and use a parameter for On and Off: set( relay, HIGH=on/LOW=off)

Most Arduino functions use "begin()", instead of "Initialize()".

Why did you use real 'tabs' ? I thought that the Arduino IDE uses spaces as tabs.

Why did you use a switch() statement in RS232Instruction().
Would this do it: Relay = RelaisNr + '0';

What is the "switch (SerialID)" doing ? Every case is the same code.

Will the library compile with Uno selected ? I'm not sure about the Serial1, Serial2, Serial3.

Yes, Serial1, Serial2 and Serial3 are only available when an Arduino MEGA2560 is selected:

#if defined(__AVR_ATmega2560__) 
		switch (SerialAddress) {
		case 0:
			Serial.begin(BaudRate);
			SerialID=0;
			break;
		case 1:
			Serial1.begin(BaudRate);
			SerialID=1;
			break;
		case 2:
			Serial2.begin(BaudRate);
			SerialID=2;
			break;
		case 3:
			Serial3.begin(BaudRate);
			SerialID=3;
			break;
		default:
			Serial.begin(BaudRate);
			SerialID=0;
			break;
		}
	#else
		Serial.begin(2400);
		SerialID=0;
	#endif

For all other boards Serial.begin is used.

Why not call the function "On", instead of Relais_ON ?
If the class would be called "Relay", it would be called with Relay.Relay_ON().

Well, that just didn't occur to me :stuck_out_tongue: I'll change that in a next version. => DONE

You could follow the digitalWrite(pin,HIGH/LOW) function, and use a parameter for On and Off: set( relay, HIGH=on/LOW=off)

Good idea... => added Set function

Most Arduino functions use "begin()", instead of "Initialize()".

hmmm, that is true. Init or initialize is something I usually use, but for standarisation begin would be more intuative. => DONE

Why did you use real 'tabs' ? I thought that the Arduino IDE uses spaces as tabs.

tabs, I love tabs :slight_smile: I use VisualMicro in VisualStudio 2010 to do both my Arduino coding as other programming languages. I'm used to use tabs.. Guilty as charged :slight_smile:

Why did you use a switch() statement in RS232Instruction().
Would this do it: Relay = RelaisNr + '0';

I never realized or tried if that would work. I'll give it a shot :slight_smile:

What is the "switch (SerialID)" doing ? Every case is the same code.

No it is not :stuck_out_tongue: If SerialID is set to 3 => Serial3.write etc.. Was the easiest way I could think of to be able to use multiple Serial ports on my MEGA

I also moved the CardAddress from begin to the seperate functions because up to 255 cards could be connected.

Alban:

What is the "switch (SerialID)" doing ? Every case is the same code.

No it is not :stuck_out_tongue: If SerialID is set to 3 => Serial3.write etc.. Was the easiest way I could think of to be able to use multiple Serial ports on my MEGA

I see, they are not the same.

The Leonardo and Micro have two serial ports, "Serial" and "Serial1". Could you add that ?

The Leonardo and Micro have two serial ports, "Serial" and "Serial1". Could you add that ?

I added the old Mega1280 to the list it is similar to the 2560.

If I understand correctly, the Leonardo and the Micro have 2 serial ports:

Note that on the Micro, the Serial class refers to USB (CDC) communication; for TTL serial on pins 0 and 1, use the Serial1 class.

So actually for my purpose there is only one correct way: Serial1. The other Serial is only accessible thru USB??
COuld anyone enlighten me on the syntax to conditional compile for those 2 boards?
I guess I could do:
#ELIF defined(__AVR_xxxxx__)

Added support for Leonardo and Micro.

I didn't think of that. You are right, the pins on the Leonardo board are only Serial1. The "Serial" is the software serial port via usb and can not be used with hardware.

The define for Leonardo and Micro is AVR_ATmega32U4

new version is online

Added to the playground