Go Down

Topic: Uni Industrial Design Project (Read 164 times) previous topic - next topic

LANCEMAN86

Now firstly I am a design student and this is my first semester having to make a product with an Arduino and I have never coded before.

My project is a Sunsmart Hat for the Cancer Council.

 




I started with someones UV sensor code which controls a RGB LED
and a temperature alarm that starts buzzing over 28 degrees on a hot day
I have also managed to stick in a 2hr timer and a code for the accelerometer
WITHOUT ERRORS!!!  :D 
but its starting to get messy to try and follow and I don't know how to alter it any further to get it to do what I need.

Any help greatly appreciated.

PaulMurrayCbr

I like to break things into objects. For instance - rather than having a colourrgb function, I will make a colourrgb object. This object holds its pin assignments internally. For your application, I'd go a little further, even, and make a UVWarningRgb that knows how the uv values map to rgb colours.

I give every object a loop() and a setup() - even if it doesn't need them - and call each loop and setup from my main loop and setup, which turns each object into a mini-sketch.

I use C++ constructors to set the pin assignments. The cute thing about this is that it becomes possible to make more than one rgb object, if you want, attached to different pins.

Code: [Select]

class WarningRGB {
    const byte rPin, gPin, bPin;

  public:
    WarningRGB(byte rPin, byte gPin, byte  bPin) :
      rPin(rPin),
      gPin(gPin),
      bPin(bPin)
    {

    }

    void setup() {
      pinMode(rPin, OUTPUT);
      pinMode(gPin, OUTPUT);
      pinMode(bPin, OUTPUT);
    }

    void loop() {
      // does nothing
    }

    void off() {
      analogWrite(rPin, 0);
      analogWrite(gPin, 0);
      analogWrite(bPin, 0);
    }

    void setWarning(float level) {
      // code to work out how to draw a colour
      // corresponding to the warning level
      // goes here
    }
};

WarningRGB uvWarning(10, 11, 12);

void setup() {
  uvWarning.setup();

}

void loop() {
  uvWarning.loop();
}


Another advantage is that this creates a natural way to break your code up into separate .h and .cpp files, if that's where you want to go.

After making this class, I'd make a "UVReader" object which reads the UV, and sets the warning level on uvWarning . Likewise, I'd have a buzzer object with on and off methods. This would be used by the sunscreentimer object, which would in turn have a "sunscreenApplied" method which would be called by the "senseHeadslap" object.  The nice thing is that if you want a second means of resetting the timer - a button or something - you just code up another object and have that call the "sunscreenApplied" method on the timer object. Likewise, the UVReader could tell the sunscreentimer object what the sun is like, and the sunscreen timer could set its timeout accordingly - perhaps even keeping a running 'uv exposure' value.

One of the benefits of using objects to do this stuff is that instead of having a rats nest of variables named "pin" and "amountA, amountB, amountUV" at the top of the sketch, variables are packaged together with the things that use them. It helps you program, because once you have coded up a class you can kinda forget about how it works internally.

There's a balance to be struck, of course. And the C++ object syntax can sometimes be a bit intimidating. But it's how I do projects with a lot of related-but-independent moving parts.

See my page Arduino the OO way.
http://paulmurraycbr.github.io/ArduinoTheOOWay.html

Go Up