-1

I am currently using relay module, but I am facing a problem that, when Arduino pins are LOW then all relay are going HIGH, so I want to use this code so that when arduino starts all pins are getting HIGH, and also can be control with char data/switch(data)/case break;/,

Here is my code:

setup() {
  if(conditionIsTrue) {
    aGroup::setup();
  } else {
    bGroup::setup();    
  }
}

loop() {
  if(conditionIsTrue) {
    aGroup::loop();
  } else {
    bGroup::loop();    
  }
}

namespace aGroup {
setup() {

    pinMode(ledPinA, OUTPUT);
    pinMode(ledPinB, OUTPUT);
    pinMode(ledPinC, OUTPUT);
    pinMode(ledPinD, OUTPUT);
    pinMode(ledPinE, OUTPUT);
    pinMode(ledPinF, OUTPUT);
    pinMode(ledPinG, OUTPUT);
    pinMode(ledPinH, OUTPUT);
    pinMode(ledPinI, OUTPUT);
    pinMode(ledPinJ, OUTPUT);
    pinMode(ledPinK, OUTPUT);
    pinMode(ledPinL, OUTPUT);

}

loop() {

  digitalWrite(ledPinA, HIGH);
  digitalWrite(ledPinB, HIGH);
  digitalWrite(ledPinC, HIGH);
  digitalWrite(ledPinD, HIGH);
  digitalWrite(ledPinE, HIGH);
  digitalWrite(ledPinF, HIGH);
  digitalWrite(ledPinG, HIGH);
  digitalWrite(ledPinH, HIGH);
  digitalWrite(ledPinI, HIGH);
  digitalWrite(ledPinJ, HIGH);
  digitalWrite(ledPinK, HIGH);
  digitalWrite(ledPinL, HIGH);

}
}

namespace bGroup {
setup() {
  Serial.begin(9600);
    pinMode(ledPinA, OUTPUT);
    pinMode(ledPinB, OUTPUT);
    pinMode(ledPinC, OUTPUT);
    pinMode(ledPinD, OUTPUT);
    pinMode(ledPinE, OUTPUT);
    pinMode(ledPinF, OUTPUT);
    pinMode(ledPinG, OUTPUT);
    pinMode(ledPinH, OUTPUT);
    pinMode(ledPinI, OUTPUT);
    pinMode(ledPinJ, OUTPUT);
    pinMode(ledPinK, OUTPUT);
    pinMode(ledPinL, OUTPUT);

}

loop() {

  char data = Serial.read();
    switch(data) 
    {
        case 'A': digitalWrite(ledPinA, HIGH);break;
        case 'B': digitalWrite(ledPinA, LOW);break;

        case 'C': digitalWrite(ledPinB, HIGH);break;
        case 'D': digitalWrite(ledPinB, LOW);break;

        case 'E': digitalWrite(ledPinC, HIGH);break;
        case 'F': digitalWrite(ledPinC, LOW);break;

        case 'G': digitalWrite(ledPinD, HIGH);break;
        case 'H': digitalWrite(ledPinD, LOW);break;

        case 'I': digitalWrite(ledPinE, HIGH);break;
        case 'J': digitalWrite(ledPinE, LOW);break;

        case 'K': digitalWrite(ledPinF, HIGH);break;
        case 'L': digitalWrite(ledPinF, LOW);break;

        case 'M': digitalWrite(ledPinG, HIGH);break;
        case 'N': digitalWrite(ledPinG, LOW);break;

        case 'O': digitalWrite(ledPinH, HIGH);break;
        case 'P': digitalWrite(ledPinH, LOW);break;

        case 'Q': digitalWrite(ledPinI, HIGH);break;
        case 'R': digitalWrite(ledPinI, LOW);break;

        case 'S': digitalWrite(ledPinJ, HIGH);break;
        case 'T': digitalWrite(ledPinJ, LOW);break;

        case 'U': digitalWrite(ledPinK, HIGH);break;
        case 'V': digitalWrite(ledPinK, LOW);break;

        case 'W': digitalWrite(ledPinL, HIGH);break;
        case 'X': digitalWrite(ledPinL, LOW);break;


    }

}
}

But this isn't work, Please help me.

| improve this question | | | | |
  • 1
    The title seems to bear absolutely no relation to the question. Could you edit one or the other so that they match, please? – Mark Smith Feb 18 '17 at 12:13
1

In first place, you have to declare the functions setup and loop as void setup() and void loop().

If you want your output pins to be high when Arduino starts then you should do that in your setup function, instead of doing it in the loop:

void setup() {

  Serial.begin(9600);

  // Step 1: configure pins as outputs
  pinMode(ledPinA, OUTPUT);
  pinMode(ledPinB, OUTPUT);
  pinMode(ledPinC, OUTPUT);
  pinMode(ledPinD, OUTPUT);
  pinMode(ledPinE, OUTPUT);
  pinMode(ledPinF, OUTPUT);
  pinMode(ledPinG, OUTPUT);
  pinMode(ledPinH, OUTPUT);
  pinMode(ledPinI, OUTPUT);
  pinMode(ledPinJ, OUTPUT);
  pinMode(ledPinK, OUTPUT);
  pinMode(ledPinL, OUTPUT);

  // Step 2: initialize pins to output HIGH
  digitalWrite(ledPinA, HIGH);
  digitalWrite(ledPinB, HIGH);
  digitalWrite(ledPinC, HIGH);
  digitalWrite(ledPinD, HIGH);
  digitalWrite(ledPinE, HIGH);
  digitalWrite(ledPinF, HIGH);
  digitalWrite(ledPinG, HIGH);
  digitalWrite(ledPinH, HIGH);
  digitalWrite(ledPinI, HIGH);
  digitalWrite(ledPinJ, HIGH);
  digitalWrite(ledPinK, HIGH);
  digitalWrite(ledPinL, HIGH);
}

Also, you can't declare a variable and assign to it the value of a function other than an object constructor. Furthermore, you should check if there's data available before trying to read it. So you have replace the following sentence:

char data = Serial.read();

With something like this:

char data = 0;
if (Serial.available() > 0) {
    data = Serial.read();
    // do your stuff
}

This should do the trick.

Last but not least: I don't know why you're using two namespaces. The aGroup looks useless to me. On top of that, I fail to see where in your code do you declare, initialise and update the conditionIsTrue variable. The same goes for all those ledPinX variables. Is there more code that you're not showing here? Is your code failing at runtime, or doesn't even compile?

| improve this answer | | | | |
  • YES SIR, it is working very good........ Thanks a lot....... – Asif Uz Zaman Feb 18 '17 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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