0

Hi im a student having problem in coding I want to set two reed switch in two direction. Example : 1. Reed switch 1 detect then reed switch 2 detect - led turns to yellow 2.If reed switch 2 detect first then reed switch 1 detect - led turns to red It should play in sequentially form. Can help me in coding? So far i have done this..

const int REED_PIN1 = 2; // Pin connected to reed switch const int REED_PIN2 = 3; // Pin connected to reed switch

const int RED = 9; // LED pin - active-high const int GREEN = 10; // LED pin - active-high const int YELLOW = 11; // LED pin - active-high

int up = 0; int down = 0;

void setup() { Serial.begin(9600); // Since the other end of the reed switch is connected to ground, we need // to pull-up the reed switch pin internally. pinMode(REED_PIN1, INPUT_PULLUP); pinMode(REED_PIN2, INPUT_PULLUP);

pinMode(RED, OUTPUT); pinMode(GREEN, OUTPUT); pinMode(YELLOW, OUTPUT);

digitalWrite(RED, LOW);
digitalWrite(GREEN, HIGH);
digitalWrite(YELLOW, LOW);
}

void loop() { int proximity1 = digitalRead(REED_PIN1); // Read the state of the switch int proximity2 = digitalRead(REED_PIN2); // Read the state of the switch while (proximity1 == LOW) // If the pin reads low, the switch is closed. {

up  = up  + 1;
Serial.print(up);
Serial.print("        ");
Serial.println(down);

if(down < 1){
digitalWrite(RED, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, LOW);

delay(1000);
}

else {
digitalWrite(RED, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, LOW);

delay(1000);
}
} while (proximity2 == LOW) // If the pin reads low, the switch is closed. { up = up - 1; Serial.print(up); Serial.print(" "); Serial.println(down); if(up < 1 && down < 1){ digitalWrite(RED, LOW); digitalWrite(GREEN, LOW); digitalWrite(YELLOW, HIGH);

delay(3000);      }
else{
digitalWrite(RED, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, LOW);

delay(1000);
}
} while (proximity2 == LOW) // If the pin reads low, the switch is closed. { down = down + 1; Serial.print(up); Serial.print(" "); Serial.println(down);

if(up > 1){
digitalWrite(RED, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, HIGH);

delay(1000);
}

else {
digitalWrite(RED, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, LOW);

delay(1000);
}
delay(1000);
}
while (proximity1 == LOW) ;// If the pin reads low, the switch is closed. { down = down - 1; Serial.print(up); Serial.print(" "); Serial.println(down); if(up < 1 && down > 1){ digitalWrite(RED, HIGH); digitalWrite(GREEN, LOW); digitalWrite(YELLOW, LOW);

delay(5000);      }
else{
digitalWrite(RED, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, LOW);
delay(5000);} } }
  • your code is really badly formatted ... do yourself a favor and format your code so that it is readable ... it will help you immensely with program creation and with debugging – jsotola Nov 25 '19 at 6:52
  • Your code is a mess. Not only is it badly formatted, but because it's badly formatted you've lost all track of your brackets, and now it's all over the place. Half the code isn't even in a function any more. – Majenko Nov 25 '19 at 9:59
  • I think, you don't understand, how while loops are written in C/C++. You can google that, to correct that part of the code. I'm pretty sure, that your current code would throw compilation errors. You can use these to fix the most urgent problems with the code. I started with the pattern "Code something, compile it, correct the errors, compile again, repeat until no errors occur". – chrisl Nov 25 '19 at 13:25
1

Sorry, but your code is complete nonsense. The following code gives you an example. I have no access to a compiler at the moment, so there might be syntax errors and it is completely untested. Also, you gave no complete description of the things you want to do. e.g when to switch on the green LED or what the up and down variables are for.

And there might also be timing problems. In order not to miss a reed state change, you should avoid adding delays and slow serial print outs.

I'm pretty sure, I did your homework. So my advice is to learn programming and try to understand. Homework was not ( only ;-) ) invented to enjoy the teacher, they help you through tests. ;-)

Your problem is similar to the decoding of a rotary encoder, so information about this subject might help you also.

const int REED_PIN1 = 2; 
const int REED_PIN2 = 3; 

// LED pins - active-high 
const int RED    =  9; 
const int GREEN  = 10;
const int YELLOW = 11;

int fixedState1 = HIGH; 
int fixedState2 = HIGH; 

void setup() 
{ 
   // If the pin reads low, the switch is closed.
   pinMode(REED_PIN1, INPUT_PULLUP); 
   pinMode(REED_PIN2, INPUT_PULLUP);

   pinMode(RED, OUTPUT); 
   pinMode(GREEN, OUTPUT); 
   pinMode(YELLOW, OUTPUT);

   digitalWrite(RED, LOW);
   digitalWrite(GREEN, HIGH);
   digitalWrite(YELLOW, LOW);
}

void loop() 
{ 
  // AS LONG AS BOTH SWITCHES ARE ON AT THE SAME TIME,
  // THE PROGRAM FLIPS OUT ;-)
  int reedState1 = digitalRead( REED_PIN1 ); 
  int reedState2 = digitalRead( REED_PIN2 ); 

  if ( readState1 == LOW && fixedState1 == HIGH )  
  {
    if ( fixedState2 == LOW )
    {
      digitalWrite( RED,    HIGH );
      digitalWrite( YELLOW, LOW  );
      digitalWrite( GREEN,  LOW  );

      fixedState2 = HIGH; 
    }
    else
    {
      // setting the green LED is just a guess 
      digitalWrite( RED,    LOW  );
      digitalWrite( YELLOW, LOW  );
      digitalWrite( GREEN,  HIGH );

      fixedState1 = LOW; 
    }
  }

  if ( readState2 == LOW && fixedState2 == HIGH ) 
  {
    if ( fixedState1 == LOW )
    {
      digitalWrite( RED,    LOW  );
      digitalWrite( YELLOW, HIGH );
      digitalWrite( GREEN,  LOW  );

      fixedState1 = HIGH; 
    }
    else
    {
      // setting the green LED is just a guess 
      digitalWrite( RED,    LOW  );
      digitalWrite( YELLOW, LOW  );
      digitalWrite( GREEN,  HIGH );

      fixedState2 = LOW; 
    }
  }
}
0

Your code is so badly formatted it makes it very difficult to follow.

A couple of things that jump out at me:

You never set REED_PIN1 or REED_PIN2 to INPUT_PULLUP mode. You should do that in setup.

You have the code that deals with REED_PIN2 commented out so that can't possibly work.

I took a first crack at improved code formatting. There result is below:

const int REED_PIN1 = 2; // Pin connected to reed switch const
int REED_PIN2 = 3; // Pin connected to reed switch

const int RED = 9; // LED pin - active-high
const int GREEN = 10; // LED pin - active-high
const int YELLOW = 11; // LED pin - active-high

int up = 0; int down = 0;

void setup() {
    Serial.begin(9600);
    // Since the other end of the reed switch is connected to ground, we need
    // to pull-up the reed switch pin internally.
    pinMode(REED_PIN1, INPUT_PULLUP);
    pinMode(REED_PIN2, INPUT_PULLUP);

    pinMode(RED, OUTPUT); pinMode(GREEN, OUTPUT); pinMode(YELLOW, OUTPUT);

    digitalWrite(RED, LOW);
    digitalWrite(GREEN, HIGH);
    digitalWrite(YELLOW, LOW);
}

void loop() {
    int proximity1 = digitalRead(REED_PIN1); // Read the state of the switch
    int proximity2 = digitalRead(REED_PIN2); // Read the state of the switch while (proximity1 == LOW) // If the pin reads low, the switch is closed.

    up  = up  + 1;
    Serial.print(up);
    Serial.print("        ");
    Serial.println(down);

    if(down < 1) {
        digitalWrite(RED, LOW);
        digitalWrite(GREEN, LOW);
        digitalWrite(YELLOW, LOW);

        delay(1000);
    }

    else {
        digitalWrite(RED, LOW);
        digitalWrite(GREEN, LOW);
        digitalWrite(YELLOW, LOW);

        delay(1000);
    }
} while (proximity2 == LOW) { // If the pin reads low, the switch is closed.
    up = up - 1;
    Serial.print(up);
    Serial.print(" ");
    Serial.println(down);
    if (up < 1 && down < 1) {
        digitalWrite(RED, LOW);
        digitalWrite(GREEN, LOW);
        digitalWrite(YELLOW, HIGH);

        delay(3000);
    }
    else {
        digitalWrite(RED, LOW);
        digitalWrite(GREEN, LOW);
        digitalWrite(YELLOW, LOW);

        delay(1000);
    }
} while (proximity2 == LOW) { // If the pin reads low, the switch is closed.
    down = down + 1;
    Serial.print(up);
    Serial.print(" ");
    Serial.println(down);

    if(up > 1) {
        digitalWrite(RED, LOW);
        digitalWrite(GREEN, LOW);
        digitalWrite(YELLOW, HIGH);

        delay(1000);
    }

    else {
        digitalWrite(RED, LOW);
        digitalWrite(GREEN, LOW);
        digitalWrite(YELLOW, LOW);

        delay(1000);
    }
    delay(1000);
}
while (proximity1 == LOW) { // If the pin reads low, the switch is closed.
    down = down - 1;
    Serial.print(up);
    Serial.print(" ");
    Serial.println(down);
    if (up < 1 && down > 1) {
        digitalWrite(RED, HIGH);
        digitalWrite(GREEN, LOW);
        digitalWrite(YELLOW, LOW);
        delay(5000);      }
    else{
        digitalWrite(RED, LOW);
        digitalWrite(GREEN, LOW);
        digitalWrite(YELLOW, LOW);
        delay(5000);

    }
}

EDIT:

After a first pass at code formatting I found the lines that are supposed to set up your REED_PIN1 and REED_PIN2 as inputs. That code got swallowed into a comment, so I didn't see it. (Everything after a // until the end of the line is a comment. You can't put the opening curly brace for a while or an if statement after a comment.)

Don't put statements on the same line as an opening brace. Don't put multiple statements on a line. Every time you enter a new level of scope (inside another set of curly braces) increase your indentation level)

These are pretty firm conventions for code formatting. There's some debate on the margins for things like where to put your braces and how many blank lines to use, but your code violates the basics of readability all over the place.

  • I have tried the coding and readjust it but i dont get the output – Subathra Nov 26 '19 at 13:15
  • I need the output like this Example : 1. Reed switch 1 detect then reed switch 2 detect - led turns to yellow 2.If reed switch 2 detect first then reed switch 1 detect - led turns to red It should play in sequentially form – Subathra Nov 26 '19 at 13:16
  • Is like car goes in one way road and what happend if the car enter in the one way from opposite site – Subathra Nov 26 '19 at 13:17
  • I can't make much sense of your twisted syntax. – Duncan C Nov 26 '19 at 14:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.