0

I am attempting to use a struct with Arduino to turn on multiple RGB LEDs to specific colors. With this sample code, I created a struct to hold a red pin num, a blue pin num, a blue value, and a red value. I am only using two of the 3 pins on the LED since I only need the colors red, blue, and purple for my application. When I run this code, the incorrect light turns on and in the incorrect color. I am not sure if I correctly understand how to use a struct in the Arduino environment. I used this source http://playground.arduino.cc/Code/Struct to find the basic syntax for a struct in Arduino. I am using it similar to how one might use an object in OOP. I am looking for clarity as to how to use the struct in Arduino and specifically I am not able to get the expected result. I expect for the LED that is connected to pin3 and pin4 to light up purple (as its red and blue pins are both set to HIGH) but instead the LED connected to pin1 and pin2 will light up red (as if pin1 is set to HIGH). Moreover, when I remove the print statements nothing turns on at all (even though this is the only change made). I have checked my wiring countless times and have determined that it is not a hardware issue. Thank you for any help that you can provide.

struct light {
  int redPin ;
  int bluePin;
  int redValue;
  int blueValue;
};

light light1;

void setup() {
  Serial.begin(9600);
  pinMode(light1.redPin, OUTPUT);
  pinMode(light1.bluePin, OUTPUT);

  light1.redPin = 3;
  light1.bluePin = 4;
  light1.redValue = HIGH;
  light1.blueValue = HIGH;
}

void loop() {
  Serial.print(light1.redPin);
  Serial.println(light1.redValue);
  Serial.print(light1.bluePin);
  Serial.println(light1.blueValue);
  digitalWrite(light1.redPin, light1.redValue);
  digitalWrite(light1.bluePin, light1.blueValue);

}
2
  • 1
    In setup You first set pinModes and then assign the value. I believe initially the light1.redPin, light1.bluePin are random because they are uninitialized so you end up setting random pins to output. Initialize pin properties first and then set the mode using pinMode.
    – Pawel
    Feb 9, 2016 at 18:34
  • What Pawel said makes all the sense... Probably that. Feb 9, 2016 at 19:34

1 Answer 1

1

As Pawel suggested, you are doing things in the wrong order. This would make a lot more sense:

  light1.redPin = 3;
  light1.bluePin = 4;
  light1.redValue = HIGH;
  light1.blueValue = HIGH;

  pinMode(light1.redPin, OUTPUT);
  pinMode(light1.bluePin, OUTPUT);

I am not sure if I correctly understand how to use a struct in the Arduino environment.

It is exactly the same as in C++.


but instead the LED connected to pin1 and pin2 will light up red (as if pin1 is set to HIGH)

Your serial prints will set pin D1 (Tx) to an output and you are seeing your serial prints as turning on the pins.

when I remove the print statements nothing turns on at all

As expected, as you are not sending data to those pins.


The default for an uninitialized global variable is zero, so I would expect that you have set pin D0 (the first pin, labelled Rx) to be an output, and then you are writing to it.


(Edited to add)

Actually, once you have done a Serial.begin the serial hardware takes over pins 0 and 1, and thus attempts to write to them fail.

The output you see on pin D1 is the Serial.print as I mentioned before, and the output on pin D0 is just the internal pull-up used to keep Rx high in the event that you are not using it just now. If you plug in an LED you will see that D0 is duller than D1.

1
  • Thank you so much I am looking into it now. Your thorough response is much appreciated. I will let you know what I return. Thanks again!
    – jaqua45
    Feb 11, 2016 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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