Total Newb. Using a Slider w/photoresister+LED+YUN

Hey Friends!

Just came across Blynk and I’m trying to do a very simple starter project (I’m new to coding/arduino).

What I would like to do is use the slider widget to create a variable number my photoresistor can use to turn an LED on.

So… when the light level is below the level I specify with the slider, I want my light to turn on.

What Is posing the biggest problem for me at the moment, is how to actually define and make use of the slider/number/virtual pins(?) in my arduino code.

If anyone can get me started that would be awesome.

Thanks!

Z

Have you been able to code this without blynk?

I.e. just using the Arduino to get the LED to turn on and off depending on the light level?

That would be your starting point, from there we can guide you in getting the blynk portion connected.

@zeeko
Let’s do next.

You’ll post your progress with these things:

  1. You’ll Make Arduino blink and LED (without Blynk)
  2. You’ll figure out how to read photoresistor data and print it to Serial Monitor (without Blynk)
  3. We will together define how to get your project online and connected to Blynk app
  4. We will together look at how to use Slider via Virtual Pin for your project.

Sounds good?

Awesome. I should probably get my arduino kit out, but the code would be something like this (just wrote this up quick, and substituted a potentiometer for a Blynk slider, of course):

int potPin = 0;    

int lightPin = 1;

int ledPin = 5;      

int lightLevel, high = 0, low = 1023;

int potValue;

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

  
}


void loop()
{
  

  
potValue = analogRead(potPin);  
outputPotValue = map(potValue, 0, 1023, 0, 100);

lightLevel = analogRead(lightPin);
outputLightValue = map(lightLevel, 0, 1023, 0, 100);


if (outputLightValue < outputPotValue) {
  digitalWrite(ledPin, HIGH);
}
else {
  digitalWrite(ledPin, LOW);
}
}

The only thing you need is switch out your potValue with a slider value from a VirtualPin. You can even get rid of the mapping for the potValue because you can do that in the app. Something like this:

int potValue;
int ldrValue;
int outputPot;
int outputLDR;

void setup()
{
  pinMode(5, OUTPUT);           //LED pin
  pinMode(A0, INPUT_PULLUP);    //Pot input
  pinMode(A1, INPUT_PULLUP);    //Photo resistor input
}

void loop()
{
  potValue = analogRead(A0);    //Put VirtualPin here and attach to slider
                                // E.g. potValue = get value from VirtualPin
  ldrValue = analogRead(A1);

  //To prevent jitter, map LDR and pot range from 0 to 20
  outputPot = map(potValue, 0, 1023, 0, 20);
  outputLDR = map(ldrValue, 0, 1023, 0, 20);

  if(outputLDR < outputPot)
  {
    digitalWrite(5, HIGH);
  }
  else
  {
    digitalWrite(5, LOW);
  }
}

This works well in my simulator (which I highly recommend by the way, http://www.virtronics.com.au/ very good simulator!)

I think you should keep the mapping in the low numbers, otherwise you get too much flickering of the lights I think. Or debounce / time the LED. Also fading is an option if you use a PWM pin.