Code verification

Hi all, I'm new to electronics as well as C/C++ and I'm looking to use Arduino R3 with Ethernet shield for a little home automation project.

I purchased my first Arduino kit yesterday but got anxious and started writing some codes now.
As I am unable to test the codes with a real Arduino just yet, I wanted someone from the community to verify whether the following codes would work for having 2 switch (Arduino as 1 switch and a 2 path switch as a second switch)

Void setup() {
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
}

void loop() {
if (serial.available() >0) {
int inByte = serial.read();

switch (inbyte) {
case "a" :
IF(12 > 0) {
digitalWrite(13,high);
digitalWrite(12,low);
break;
  }
IF(13 > 0) {
digitalWrite(13,low);
digitalWrite(12,high);
break;
  }
    }
  }
}

You may not have the hardware yet, but that is no excuse for not making sure that the code at least compiles. It doesn't. Fix those issues, and try again.

IF(13 > 0) {

Are there some values of 13 that might not be greater than 0?

Your right my apologies for my hastiness,

I took bits and bobs from other codes but didn't realise the language was case sensitive :blush:

I dashed it into the Arduino compiler and sorted that out.

as for the "if" statement I had assumed that low represented 0. I changed it to 13>12 so it will decide based both pin outputs now.

void setup() {
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
}

void loop() {
if (Serial.available() >0) {
int inByte = Serial.read();

switch (inByte) {
case 'a' :
if(12 > 13) {
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
break;
  }
else {
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
break;
  }
    }
  }
}

if(12 > 13)

That can never be.... do you mean the values of pins 12 & 13?

I changed it to 13>12 so it will decide based both pin outputs now.

Is 12 greater than 13 in your universe? It isn't in mine. Instead of explaining what you did to the code, explain what you are trying to have the code do. Testing two constants for equality or inequality makes no sense. Setting one pin HIGH, after setting the other one LOW, might make sense.

JimboZA:
That can never be.... do you mean the values of pins 12 & 13?

exactly what I'm trying to get. Guess I'm missing something to represent that... back to the reference section to see what I'm missing.

PaulS:
[quoteInstead of explaining what you did to the code, explain what you are trying to have the code do.

I already mentioned what I was trying to do on my first post... Use the Arduino UNO R3 board as a 2 path switch so I can turn the LED on and off from either the computer (later using ethernet shield) or manually by flipping the switch.

I already mentioned what I was trying to do on my first post... Use the Arduino UNO R3 board as a 2 path switch so I can turn the LED on and off from either the computer (later using ethernet shield) or manually by flipping the switch.

What you want to do does not make sense. The switch is wired as though it is for input. It is wired as though it is two switches, where only one can be HIGH at a time.

Bhavesh_k:

JimboZA:
That can never be.... do you mean the values of pins 12 & 13?

exactly what I'm trying to get. Guess I'm missing something to represent that... back to the reference section to see what I'm missing.

Perhaps
  if (digitalRead (12) && !digitalRead (13))is what you meant??

PaulS:
[quoteInstead of explaining what you did to the code, explain what you are trying to have the code do.

I already mentioned what I was trying to do on my first post... Use the Arduino UNO R3 board as a 2 path switch so I can turn the LED on and off from either the computer (later using ethernet shield) or manually by flipping the switch.

You are not understanding microcontrollers.

Switch goes to one pin and ground, use INPUT_PULLUP on that pin and that's an
input.

LED and resistor go on a separate pin, which is an output.

The code looks at the info from the switch and from the serial coming in to
make a decision, then update the LED output.

Your circuit is mixing inputs and outputs in a confused way.

Thanks mark,

Got it to work with digitalRead :D, this is the final code I ended up with

void setup() {

pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
Serial.begin(9600);
}

void loop() {

if (Serial.available() >0) {
int inByte = Serial.read();

switch (inByte) {
  case 'a' :
  if(digitalRead(13) == HIGH) {
  digitalWrite(13, LOW);
  digitalWrite(12, HIGH);
  break;
  }
  else { 
  digitalWrite(13, HIGH);
  digitalWrite(12, LOW);
  break;
  }
}
}
}

With this I was able to turn the led on and off independently from either the computer or physically from the switch.
Kind of like the picture below.

(Arduino acting as both the power supply and 2 path switch)

Going to use the same concept using an Ethernet shield and a Relay to control the lights around the house (eventually) :slight_smile:

Thanks for all the help with this