0

Bear with me as I'm fairly adept at C# but I am having a problem understanding how to get an array iterated.

I am writing a program to test relay boards, it will eventually have an input mechanism to select the number of relays on the board. For now, i just need to get an array iterated to move on with the code.

The biggest relay board I've seen has 16 relays, so....

#define Relay1  2  // Arduino Digital I/O pin numbers for UNO R3
#define Relay2  3
#define Relay3  4
#define Relay4  5
#define Relay5  7  
#define Relay6  8
#define Relay7  9
#define Relay8  10
#define Relay9  11  
#define Relay10  12
#define Relay11  13
#define Relay12  A0
#define Relay13  A1 
#define Relay14  A2
#define Relay15  A3
#define Relay16  A4

int relays[]{ Relay1,Relay2,Relay3,Relay4,
    Relay5,Relay6,Relay7,Relay8 };
int maxRelayCount = 8;
char rx_byte = 0;

void setup()   //Initializes all variables and settings
{
  Serial.begin(9600);
 // create an input for the amount of relays on the board
//hardcoded for now

//Set pins to OFF & declare pins as OUTPUTS
 for(int i = 0; i < maxRelayCount; i++)
 {
   digitalWrite(relays[i], RELAY_OFF);
   pinMode(relays[i], OUTPUT);
   //How to convert i to a string in c++?
   //Serial.print("Relay " +[i] + " set HIGH and as OUTPUTS");
 }

 //Check that all relays are inactive at Reset
 delay(4000); 

}

So there are two issues. If I remove the loop and just hard code 8 times to set HIGH and OUTPUT it works fine, but the loop does not and won't upload to the Arduino, thus the reason I believe it is wrong. Secondary question is how to convert the counter i to a string to concatenate the message

  • 1
    Why not break the print statement into three parts; Serial.print("Relay["); Serial.print(i); Serial.print("] set HIGH and OUTPUT"); – Mikael Patel Apr 2 '16 at 18:27
1

As noted earlier, RELAY_OFF needs a defined value.

Note, rather than using clumsy list of #define statements to define a bunch of integer constants, you can use C's enum declaration to define them, as shown in the following sketch, which produces the output

Relay 1 should be OFF
Relay 2 should be OFF
Relay 3 should be OFF
Relay 4 should be OFF
Relay 5 should be OFF
Relay 6 should be OFF
Relay 7 should be OFF
Relay 8 should be OFF

when it is run.

#include <Streaming.h>

// Arduino Digital I/O pin numbers for UNO R3
enum { Relay1=2, Relay2=3, Relay3=4, Relay4=5, Relay5=7, Relay6=8,
       Relay7=9,   Relay8=10,  Relay9=11,  Relay10=12,  Relay11=13,
       Relay12=A0, Relay13=A1, Relay14=A2, Relay15=A3,  Relay16=A4};

int relays[] = {Relay1,Relay2,Relay3,Relay4,Relay5,Relay6,Relay7,Relay8};
// Number of relays in the array
enum { maxRelayCount = sizeof relays / sizeof relays[0] };
enum { RELAY_OFF = HIGH };  // Set LOW or HIGH as appropriate
char rx_byte = 0;

void setup() {          // Initialize variables and settings
  Serial.begin(9600);
  //Set pins to OFF & declare pins as OUTPUTS
  for(int i = 0; i < maxRelayCount; ++i) {
    digitalWrite(relays[i], RELAY_OFF);
    pinMode(relays[i], OUTPUT);
    // Serial.print("Relay " +[i] + " set HIGH and as OUTPUTS");
    Serial << "Relay " << i+1 << " should be OFF" << endl;
  }

 // Check that all relays are inactive at Reset
 delay(4000);
}

void loop() {}

Note, the Streaming.h library adds some “syntactic sugar” to Arduino C. At compile time it converts C++-like << Serial stream operators to Serial.print statements, without increasing code size. You can install it by unzipping Streaming5.zip from arduiniana.org in your sketchbook/libraries directory.

  • I didn't put the RELAY_ON and RELAY_OFF defines in the question, but I like the usage of enum and the streaming library – dinotom Apr 2 '16 at 19:39
2

the loop does not and won't upload to the Arduino

Most likely because RELAY_OFF has never been defined anywhere. Change that to HIGH (if that is what your relays need to turn off), or define it somewhere.

Also there is no loop() function - is that just you being economical with the paste?

Secondary question is how to convert the counter i to a string to concatenate the message.

Simple: don't. There's no need.

Serial.print("Relay ");
Serial.print(i);
Serial.println(" set HIGH and as OUTPUT");

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.