Question

First off, I'm not looking for exact code, just some help creating an algorithm. Here we go...

I have an arduino with an ethernet shield attached that is setup to make a HTTP request every few minutes. The response to that request is a simple JSON array {"new":0} or {"new":1} I wont go into great detail but essentially, I have a thing out on the web that when updated, this JSON array changes to 1 and when that updated has been acknowledged, i.e. a user logs into a backend, that JSON array changes back to 0.

What I need the arduino to do is when the JSON array is equal to 1, fade an LED on and off until that JSON array is equal to 0 again.

What I am having trouble with, is devising an algorithm that will fade the LED while at the same time continue to send HTTP requests.

Was it helpful?

Solution

The basic psuedo-code outline is something like:

int PWM;
int minute_ctr;

loop()
{
   if (minute_ctr > 1000)
   {
      minute_ctr = 0;
      check HTML page for 1/0
   }

   if (page == 1)
      PWM++;
      set PWM for LED (analogWrite) change direction when PWM = 0 or 255
   else 
      set LED off
   end if

   delay(10)  

   minute_ctr += 10;
}

You will need to fill-in all of the details.

OTHER TIPS

I would recommend looking at the Simple Timer Library it will basically do as @JackCColeman suggests, but in a cleaner/simpler fashion, behind the scene. similar to an interrupt as @Morrison Chang, suggests.

#include <SimpleTimer.h>
SimpleTimer timer;

// a function to be executed periodically
// by the below timer.run in the main loop
void change_pwm() {
  if (page == 1)
    PWM++;
    set PWM for LED (analogWrite) change direction when PWM = 0 or 255
  else 
    set LED off
  end if
}

void setup() {
    Serial.begin(9600);
    timer.setInterval(10, change_pwm);
}

void loop() {
    timer.run();
    //...along with all the other stuff.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top