Skip to main content
Arduino

Return to Answer

replaced http://arduino.stackexchange.com/ with https://arduino.stackexchange.com/
Source Link

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. .

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.

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.

Source Link

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.

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /