Assignment 3: Fading

The assignment seemed really confusing when I first read it, but Professor Shiloh cleared out a few things and when I just took a bit of time to read it, it was pretty straightforward.

Now the assignment asks us for two things – a fade program in the setup() function, and one in the loop() function. Of course I started off with the one in the loop() function. After reading the fade tutorial, I had my own go at coding it from scratch. This is how it looked; it’s pretty straightforward in my opinion:

int pinBlue=9;
int bright=0;
int fadeAmt=5;

void setup() {
    pinMode(pinBlue, OUTPUT);
}

void loop() {
   analogWrite(pinBlue, bright);
   bright += fadeAmt;
      if(bright>254)
     {
         fadeAmt=-fadeAmt;
     }
     if(bright<1)
     {
         fadeAmt=-fadeAmt;
     }
  delay(50); //to get a neat transition, or else it would just blink?
}

Here’s how the connections looked like (note that the other LED is also connected here in the diagram, but not in the video that will soon follow) :

Circuit diagram

Everything worked perfectly! Here’s the video (please excuse the laugh, aliens to this subject find our work amusing) :

Then I had my crack at trying to make the LED fade by having the code in the setup() function. 

TL;DR – I failed.

Here’s how I tried – I first thought about what the assignment asked for (I hope I have understood it correctly). I reasoned out that the assignment pretty much asked that we accomplish the same thing as what we did in the loop() function. I struggled with how to approach the problem. It seemed to me as though we were being asked to make the light fade in and out but with using an iterative statement (i.e, a loop). I figured that since I am not allowed to loop it, I would try using a jump statement to go back to a point in the code and repeat doing so until a certain condition is met. In theory, that is a LOOP, but without using a loop statement (Seems a bit odd, yeah?). I have some programming experience in Java, so I had look up how exactly the syntax worked in C++. Here’s the code I tried to use : 

int pinRed=10;
int bright=0;
int fadeAmt=5;

void setup() {
   pinMode(pinRed, OUTPUT);

start:
   int i=1;
   i++;
bright += fadeAmt;
   if(bright>254)
   {
      fadeAmt=-fadeAmt;
   }
if(bright<2)
  {
      fadeAmt=-fadeAmt;
  }
   delay(50);
if (i<50)
{
   goto start;

}}

I agree, that this fundamentally is a loop, but I couldn’t figure out any other way. Also, this never worked and the LED didn’t light up at all. I reasoned out that this is because the setup() function has to complete execution before the circuit actually begins to work the way we intend to. Or it could simply be that my code has some errors. Or it could be something else, I am not really sure. 

I hope to have another go at this sometime and see if I can figure out what the answer is. 

 

Leave a Reply

Your email address will not be published. Required fields are marked *