i have been told by LUCA BRANZI that the code is wrong and that this is the right one
// Maurice Ribble
// 4-6-2008
// http://www.glacialwanderer.com/hobbyrobotics
// This code just lets you turn a digital out pin on and off. That's
// all that is needed to verify a relay curcuit is working.
// Press the space bar to toggle the relay on and off.
#define RELAY_PIN 3
void setup()
{
pinMode(RELAY_PIN, OUTPUT);
Serial.begin(9600); // open serial
Serial.println("Press the spacebar to toggle relay on/off");
}
void loop()
{
static int relayVal = 0;
int cmd;
while (Serial.available() > 0)
{
cmd = Serial.read();
switch (cmd)
{
case ' ':
{
relayVal ^= 1; // xor current value with 1 (causes value to toggle)
if (relayVal)
Serial.println("Relay on");
else
Serial.println("Relay off");
break;
}
default:
{
Serial.println("Press the spacebar to toggle relay on/off");
}
}
if (relayVal)
digitalWrite(RELAY_PIN, HIGH);
else
digitalWrite(RELAY_PIN, LOW);
}
}
By the way i decided not to waste time on this code since what i wanted to do is to control the lamp by using a button...I managed to controll 12V leds using this code:
//interi datamatrix
int datamx0 = 10;
int datamx1 = 8;
int datamx2 = 3;
int datamx3 = 2;
//interi bottoni di scorrimento
int btn0 = 13;
int btn1 = 12;
int btn2 = 11;
int btn3 = 4;
int val = 0;
void setup()
{
pinMode (datamx0 , OUTPUT);
pinMode (datamx1 , OUTPUT);
pinMode (datamx2 , OUTPUT);
pinMode (datamx3 , OUTPUT);
pinMode (btn0, INPUT);
pinMode (btn1, INPUT);
pinMode (btn2, INPUT);
pinMode (btn3, INPUT);
}
void loop() {
//btn0
{
val = digitalRead(btn0); // leggo lo stato del bottone
if (val == HIGH) { // se il bottone da come valore HIGH
digitalWrite(datamx0,LOW); // il led è spento
}
else {
digitalWrite(datamx0, HIGH); // altrimenti è acceso
}
}
//btn1
{
val = digitalRead(btn1);
if (val == HIGH) {
digitalWrite(datamx1,LOW);
}
else {
digitalWrite(datamx1, HIGH);
}
}
//btn2
{
val = digitalRead(btn2);
if (val == HIGH) {
digitalWrite(datamx2,LOW);
}
else {
digitalWrite(datamx2, HIGH);
}
}
//btn3
{
val = digitalRead(btn3);
if (val == HIGH) {
digitalWrite(datamx3,LOW);
}
else {
digitalWrite(datamx3, HIGH);
}
}
}
And everything was fine expect that the light i needed was not enough and that's when i started tryng 230V bulbs...now the circuit seem to work, and what i'm trying to do is to do the same i did with the leds but with Light bulb instead of the 12V leds....
Do you think that this code would eventually work?
Thanks for your preciuos Help....i really mean it!