activating 2 switches with serial feedback

Hi everyone,
My programming knowledge is a bit shakey at the moment, so please bare with me while i bring myself up to speed.
Basically I want to be able to program my arduino to open and close my garage doors by activating a switch on a latch for say less than 1 second. But I also want the arduino to be able to pass back to the connected computer a serial response such as 'Garage Door in Motion'.

At present I have :

/*
 * Garage Door Opener
 *
 * Using a quick pulse to open and close the garage doors,
 * the 1st garage door is called garageone, and the second is garagetwo
 * Pass 'garageone' to activate the 1st garage door and 'garagetwo' to activate the 2nd

 */

int garageone = 1;                // garage door one to pin 1
int garagetwo = 2;                           // Garage Door two to pin 2
int variable = 0;                          // Variable

void setup()
{
  pinMode(garageone, OUTPUT);      // sets the digital pin as output
  pinMode(garagetwo, OUTPUT);      // sets the digital pin as output
  Serial.begin(9600);  // start serial for output
}

void loop()                     // main app
{
   if (Serial.available() > 0) {
// read the incoming byte:  
variable = Serial.read();

if (garageone == variable) 
{
 digitalWrite(garageone, HIGH);    // opens the 1st garage door
  delay(500);                  
  digitalWrite(garageone, LOW);
  Serial.println("Garage Door One Is in Motion");   // Feedback 
  variable = 0;
}
  
if (garagetwo == variable) 
{
 digitalWrite(garagetwo, HIGH);    // opens the 2nd garage door
  delay(500);                  
  digitalWrite(garagetwo, LOW);
   Serial.println("Garage Door Two Is in Motion");   // Feedback 
  variable = 0;
}

}


However I cant understand why it wont compile or work, does anyone mind offering insite?

Thanks
Mathew

You are missing a closing brace } at the end of the file

wow that was easy enough.

Thanks :slight_smile:

still doesnt seem to do anything,
Ive set garageone to pin 13 to see if it illuminates the LED and nothing.
nor do i get any return from the board.

The code expects binary 1 and 2. if you are sending ascii digits your code needs to be something like:
variable = Serial.read() – '0'; // convert ascii to binary

The code expects binary 1 and 2. if you are sending ascii digits your code needs to be something like:
variable = Serial.read() – '0'; // convert ascii to binary

What do you mean?

If you send the digit 1 from a program such as the Arduino serial monitor, you are sending a character that has a decimal value of 49. A google search on ASCII will provide more info on this. Your code was expecting a value of 1 or 2, not ascii '1' (49) or ascii '2' (50);

for your code to work, you need to either convert the incoming ascii to decimal by subtracting 48. 48 is the ascii value of '0' so subtracting '0' from the incoming serial data converts it to its decimal value.

humm would it not be better to send '1' for the 1st garage door and '2' for the send garage door?

so:

if (1 == variable)

What program are you using to send the data?

at present just Arduino 10 - but the end target is to have a webpage that can control the board - however i havent made that bit yet, so for now Arduino 10

The values you transmit using the arduino serial send are ascii. so sending 1 will actually send a byte with the value of 49. This may be a little confusing at first but its not a problem as long as your software does the translation. Did you try it with the change I suggested in reply#4 above?

The values you transmit using the arduino serial send are ascii. so sending 1 will actually send a byte with the value of 49. This may be a little confusing at first but its not a problem as long as your software does the translation. Did you try it with the change I suggested in reply#4 above?

I dont really understand what you mean from post 4. I understand that the characters i am sending from the computer are in ascii form. but I dont understand why they need to be converted. - Originally I used garageone and garagetwo just as reference points so I knew what I was sending to the board. Do i need to adapt the code somewhere to read ascii?

Yes, you do need to adapt the code so it checks the ascii values. You were also using pin 1 which is used by the arduino serial port.

I hope the following code is helps clarify what needs to happen:

// pins 0 and 1 are used by the serial port so we use pins 2 and 3 for the doors 
int garageOnePin  = 2;       // garage door one to pin 2
int garageTwoPin  = 3;          // garage Door two to pin 3


void setup()
{
  pinMode(garageOnePin, OUTPUT);      // sets the digital pin as output
  pinMode(garageTwoPin, OUTPUT);      // sets the digital pin as output
  Serial.begin(9600);  // start serial for output
}

void loop()                     // main app
{
int variable;

  if (Serial.available() > 0) {
    // read the incoming byte:  
    variable = Serial.read();

    if ('1' == variable)  // ascii '1' triggers door 1
    {
      digitalWrite(garageOnePin, HIGH);    // opens the 1st garage door
      delay(500);                  
      digitalWrite(garageOnePin, LOW);
      Serial.println("Garage Door One Is in Motion");   // Feedback 
      variable = 0;
    }

    if ('2' == variable) 
    {
      digitalWrite(garageTwoPin, HIGH);    // opens the 2nd garage door
      delay(500);                  
      digitalWrite(garageTwoPin, LOW);
      Serial.println("Garage Door Two Is in Motion");   // Feedback 
      variable = 0;
    }
  }
}

fantastic,
thank you very much for your help :slight_smile: