 # Using a sensors feedback to regulate two separate pwm pins

Hi all

As I`m trying to use one sensor`s feedback to regulate two pwm pins I find it a bit complicated to make it jump to the next 'else if statement after the first ‘if’. Anyone could help me with overcome this issue?

So this is the part where i`m stuck at. The first part work fine, but it does not execute the second part when the sensors feedback changes and the brightness6k is not 0 anymore.

``````  if (lux<3958 && brightness6k ==0  )
{
brightness1k++;
}

if (lux>3958 && brightness6k ==0 )
{
brightness1k--;
}

else if (lux == 24200 )
{
brightness6k++;
}
else if (lux > 24200)
{
brightness6k--;
}

}
``````

You have two if statements, one of which has 2 else if statements. There are 4 discrete states, so you should one if statement with 3 else if statements.

When experiencing problems with if/else it is a good idea to print the values being tested before the test and to print a message from each code block so that you know what is being executed. It is also often a good idea to work through the if/else sequence on paper to help understand what will happen when the variables have certain values.

``````else if (lux == 24200 )
``````

Unless you are very sure of the values to be expected, such as HIGH/LOW or true/false it is not usually a good idea to test for equality.

Incidentally, what should happen when lux equals 3958 ?

When lux = 3958; it should basically stop regulating that value which 3958, but im working with light so the values are changing all the time +-5 lux.

@ PaulS
I have done one if statement with 3 else if statements, but it behaves exactly the same.

Start by writing down in normal English what you want to happen.
After that it should be a pretty small step to get that into correct computer code.

You test all the time for a value of brightness, and a brightness6k. You also make changes to a brightness1k. What is that all supposed to do?

Also don’t test for equality of a value, especially not as it’s changing all the time as you say yourself. Test for a range, of +/- 50 or so.

I have done one if statement with 3 else if statements, but it behaves exactly the same.

You changed the code, but did not post it. It does something that you have not defined (or proven by showing the output from the program). You expect it to do something that you have not defined.

that is the modification i have done.

``````if (lux<3958 && brightness6k ==0  )
{
brightness1k++;
}

else  if (lux>3958 && brightness6k ==0 )
{
brightness1k--;
}
else if (lux < 24200 && brightness6k !=0  )
{
brightness6k++;
}
else if (lux > 24200 && brightness6k !=0)
{
brightness6k--;
}
return;
}
/*
``````
1. I’d use >= instead of > (or <= instead of <) as otherwise if lux happens to equal the value the state is missed.
2. does it work? If not, what does it do and what do you expect it to do?

What it does now is after entering my first value, it does maintain it around 3958, but then when i enter my second value, it tries to maintain it around 24150, but after 1 or 2 seconds it put brightness6k= 0 for some reason. Here is the whole code

What i want to do is when i enter the second value which is 6000… it needs to execute this brightness1k=2140;
brightness6k=4020;

and then keep lux around 24200

``````void setup()
{
pwm.begin();
pwm.setPWMFreq(1600);
Serial.begin(115200);
Wire.begin();
lightMeter.begin();
//Serial.println(F("BH1750 Test"));
while (Serial.available()==0)
{
pwm.setPWM(11, 0, 4096);
pwm.setPWM(7, 0, 4096);

}

}

void loop(){
delay(160);
Serial.print("Light= ");
Serial.println(lux);
Serial.println("lux");

//send data only when to recieve data
if (Serial.available() > 0)
{
int ch1 = Serial.parseInt();
ch1 = constrain(ch1,0,6000);
if(ch1==1000){
brightness1k=2140;
}
else {
brightness1k=0;

}
if (ch1==6000) {
brightness1k=2140;
brightness6k=4020;
}
else {
brightness6k=0;

}
delay(5);
}
pwm.setPWM(11, 0, brightness1k);
pwm.setPWM(7, 0, brightness6k);

Serial.println("  ");
Serial.print("brightness1k=");
Serial.println(brightness1k);

if (lux<=3958 && brightness6k ==0  )
{
brightness1k++;
}

else  if (lux>=3958 && brightness6k ==0 )
{
brightness1k--;
}
else if (lux <= 24200 && brightness6k !=0  )
{
brightness6k++;
}
else if (lux >= 24200 && brightness6k !=0)
{
brightness6k--;
}
return;
}
``````

In your IDE, press ctrl-T. That formats your code and makes it readable (it corrects indentation and so); now it's a mess. Readable code is much easier to debug.

``````  while (Serial.available()==0)
{
pwm.setPWM(11, 0, 4096);
pwm.setPWM(7, 0, 4096);

}
``````

Why do you need to call the setPWM() method over and over? Don't you believe that ONE call is sufficient?

``````  if(ch1==1000){
brightness1k=2140;
}
else {
brightness1k=0;

}
if (ch1==6000) {
brightness1k=2140;
brightness6k=4020;
}
else {
brightness6k=0;

}
``````

Why do you have two tests for ch1 equal 6000, while doing some identical and some different stuff?

bakersalama:
Here is the whole code

No, it's not the whole code. It's only part of it. And the error is probably in the part you didn't post.

bakersalama:
but after 1 or 2 seconds it put brightness6k= 0 for some reason.

How do you know this? The value is not printed or so and nothing in the posted code that shows it will behave like that.

what you suggested Wvmarle has worked for me after changing the values of the lux… this is the code i have, it might be helpful for someone.

``````#include <Wire.h>
#include <BH1750.h>
BH1750 lightMeter;
//1000lux = 2170 bits
//6000lux= 4020 bits

int brightness6k;
int brightness1k;
//int sensor = 4080;

//int valuechanged;

bool target1k= true;

bool switch1k = false;
//int brightensschange= 2080;

void setup()
{
pwm.begin();
pwm.setPWMFreq(1600);
Serial.begin(115200);
Wire.begin();
lightMeter.begin();
//Serial.println(F("BH1750 Test"));
while (Serial.available()==0)
{
pwm.setPWM(11, 0, 4096);
pwm.setPWM(7, 0, 4096);

}

}

void loop(){
delay(160);
Serial.print("Light= ");
Serial.println(lux);
Serial.println("lux");

//send data only when to recieve data
if (Serial.available() > 0)
{
int ch1 = Serial.parseInt();
ch1 = constrain(ch1,0,6000);
if(ch1==1000){
brightness1k=1985;
}
else {
brightness1k=0;

}
if (ch1==6000) {
brightness1k=1985;
brightness6k=4000;
}
else {
brightness6k=0;

}
delay(5);
}
pwm.setPWM(11, 0, brightness1k);
pwm.setPWM(7, 0, brightness6k);

Serial.println("  ");
Serial.print("brightness1k=");
Serial.println(brightness1k);

if (lux<=3803 && brightness6k ==0  )
{
brightness1k++;
}

else  if (lux>=3798 && brightness6k ==0 )
{
brightness1k--;
}
else if (lux <= 23300 && brightness6k !=0  )
{
brightness6k++;
}
else if (lux >= 23295 && brightness6k !=0)
{
brightness6k--;
}
}
``````