Chat freely about anything...

User avatar
By Barnabybear
#34364 Hi, I can't get this bit of code to pass the first "if" check (it always returns nZone = 17), what am I doing wrong?
Code: Select allrLen = (random(6));  // random number 0->5
if  (rLen == 0 || 1 || 2 ){  // if equal to 0, 1 or 2
nZone = 17;  //  nZone = 17, if == 0, 1 or 2
}
else if  (rLen == 3 || 4){  // if equal to 3 or 4
nZone = 16;  //  nZone = 16, if == 3 or 4
}
else {
nZone = 15;  //  nZone = 15, if == 5 (or none of the above)
{

I've got round by using
Code: Select allrLen = (random(6));
if  (rLen < 3 ){
nZone = 17;
}
else if  (rLen <5){
nZone = 16;
}
else {
nZone - 15;
{

Which is fine for this appilcation. But thought it should have worked.

Thanks for looking.
User avatar
By Mmiscool
#34365 Should the if statement have multiple evaluations in it.

Code: Select allif  (rLen == 0 || rLen ==1 ||rLen == 2 ){
User avatar
By Barnabybear
#34401
Mmiscool wrote:Should the if statement have multiple evaluations in it.

Code: Select allif  (rLen == 0 || rLen ==1 ||rLen == 2 ){

Yep thats it. To long sat in a darkened room brain gone.

eduperez wrote:I would also consider using a switch / case statement: https://www.arduino.cc/en/Reference/SwitchCase

Hi, thanks. As it sets a variable for the number of times a for loop circles (its a lighting effet that 50% of the time runs the full effect, 34% and 14% runs a shortened effect). Is there an advantage to switch case over "if and else if"? Or generaly a better way to do this?
Code: Select allrLen = (random(6));
if  (rLen == 0 || rLen ==  1 || rLen ==  2 ){
nZone = 17;
}
else if  (rLen == 3 || rLen ==  4){
nZone = 16;
}
else {
nZone = 15;
{

for (int Zone = 12; Zone <= nZone; Zone++) {