Could someone advise on tidying up my code?

Hi there, could someone please help me. I either need a better way (if there is one) or can my code not be improved?

I have an RF reciever witch operates off of 5v and the switching voltage is also 5v. The switched pins go to my arduino pins 9,10,11 and 12.

I possible I would like to read the pins and then jump to a section if the pin goes high? So the sketch isn't running through every line of code, it is just watching for the pin to go high.

Here is my code:

int aPin = 12;  // choose the input pin (for a A signal)
int aVal = 0;  // variable for reading the pin status
int bPin = 11;  // choose the input pin (for a B signal)
int bVal = 0;  // variable for reading the pin status
int cPin = 10;  // choose the input pin (for a C signal)
int cVal = 0;  // variable for reading the pin status
int dPin = 9;  // choose the input pin (for a D signal)
int dVal = 0;  // variable for reading the pin status

void setup() {
  Serial.begin(9600);
  pinMode(aPin, INPUT);    // declare input
  pinMode(bPin, INPUT);    // declare input
  pinMode(cPin, INPUT);    // declare input
  pinMode(dPin, INPUT);    // declare input
}

void loop(){
  aVal = digitalRead(aPin);  // read input value
  if (aVal == HIGH) {         // check if the input is HIGH (A)
    Serial.println("A");  // output A
  }
  bVal = digitalRead(bPin);  // read input value
  if (bVal == HIGH) {         // check if the input is HIGH (B)
    Serial.println("B");  // output B
  }
  cVal = digitalRead(cPin);  // read input value
  if (cVal == HIGH) {         // check if the input is HIGH (C)
    Serial.println("C");  // output C
  }
  dVal = digitalRead(dPin);  // read input value
  if (dVal == HIGH) {         // check if the input is HIGH (D)
    Serial.println("D");  // output D
  }
}

Thanks.
Bert.

You would need to use an interrupt to watch for the pin(s) to go high.

Using diodes, it is possible to connect more than one signal wire to both an interrupt pin (pin 2 or 3) and a regular. Do this for all 4 wires. When the interrupt is triggered, read the 4 regular pins to see which one generated the input.

Unless the code is doing something else, meanwhile, why do you want to go the interrupt route?

How would that setup work? With the 4 trigger wires? and the interupt?

I'll be adding a bluetooth module aswell that will receive bytes via serial and these can also print the letters A B C and D to serial.

Just a note: The serial print of A B C and D will be replaced with outputs to relays, I am using print line just to test the code until my relay board arrives.

Thank you.

Why do you think that polling won't be fast enough? Lets get that sorted out first.

Using interrupts adds another level of complexity to the picture.

So my code is fine as it stands? If your saying it is thanks for the help :smiley:

I will have a read up on interrupts for the next time I need one.

Thanks again.

here is tidy as i could think of


int inputPin = {9, 10, 11, 12}; //input pins
boolean value[4] = {0, 0, 0, 0}; //values of pins as HIGH or LOW only
char displayChar[4] = {‘A’, ‘B’, ‘C’, ‘D’}; //values to print to screen

void setup()
{
Serial.begin(9600);

for(int i = 0; i < 4; i++)
{
pinMode(inputPin*, INPUT); //set pins to inputs*

  • }*
    }
    void loop()
    {

  • for(int j = 0; j < 4; j++) //set values from pins*

  • {*

  • value[j] = digitalRead(inputPin[j]);*

  • delay(1000);*

  • }*

  • for(int k = 0; k < 4; k++) //display pins as A - D*

  • {*

  • if(value[k] == HIGH)*

  • {*

  • Serial.print(displayChar[k]);*

  • Serial.print(".\n");*

  • }*

  • }*
    }

pinMode(inputPin, INPUT);              //set pins to inputs

Oopsie!

Can you please remember to use the code (#) icon in the editor’s toolbar when posting code.

const int inputPin[] = {9, 10, 11, 12};
const char displayChar[4] = {'A', 'B', 'C', 'D'};
#define SIZEOF(x) (sizeof(x)/sizeof(x[0]))

void setup()
{
  Serial.begin(9600);
  
  for(int i = 0; i < SIZEOF(inputPin); ++i)
  {
    pinMode(inputPin [i], INPUT);
  }
}

void loop()
{
  for(int i = 0; i < SIZEOF(inputPin); ++i)
  {
    if (digitalRead(inputPin[i]))
      Serial.println(displayChar[i]);
  }
}