If inside a if?

I have learned a lot reading on here, but I have gotten stumped. Im trying to have one output be ran by two inputs. I have a LED hooked up right now as the output for testing.
GOAL
When brakesw is pressed and held, brake is high and twostep is low.
If V1 is in between the set voltage while brakesw is held, brake will go low for set amount of time(bump) and twostep will stay low. Then after the bump time brake goes back to high and twostep still low until brakesw is released.

I have tried a few things, learned else if is not what i need. Last two tries I had one volt to the brake with brakesw was pressed. This version the light would flash on with the bump timer when brakesw pressed.
If I remove the line of code involving V1 and the bump the switch will work like a normal switch so I'm pretty sure my issue is there but I'm not sure what to try next. Hopefully the code post correctly.

int brake=12;
int brakesw=9;
int twostep=7;
float V1;
int readVal;
int dt=2;
int sw=A1;
int bump=2000;

void setup() {
  pinMode(brake,OUTPUT);
  digitalWrite(brake,LOW);
  pinMode(sw,INPUT);
  pinMode(twostep,OUTPUT);
  digitalWrite(twostep,HIGH);
  pinMode(sw,INPUT);
  Serial.begin(9600);
}

void loop() {
  readVal=analogRead(sw);
  V1=(5./1023.)*readVal;
  Serial.println(V1);
  
if (digitalRead(brakesw)==HIGH)
   {digitalWrite(brake,HIGH);
   digitalWrite(twostep,LOW);
      if (analogRead(V1>=4.0 && V1<=4.5))
      digitalWrite(brake,LOW);
      delay(bump);}
      else {(digitalWrite(brake,LOW));
       (digitalWrite(twostep,HIGH));}

Yes, but not completely.

  if (digitalRead(brakesw) == HIGH)
  {
    digitalWrite(brake, HIGH);
    digitalWrite(twostep, LOW);
    if (analogRead(V1 >= 4.0 && V1 <= 4.5))
    {
      digitalWrite(brake, LOW);
      delay(bump);
    }
    digitalWrite(brake, HIGH);
  }
  else
  {
    (digitalWrite(brake, LOW));
    (digitalWrite(twostep, HIGH));
  }

Oops

Indeed!

You forgot to use Tools -> Auto Format before Edit -> Copy for Forum.

There is also a missing '}' (or extra '{') somewhere.

You have pinMode() twice for 'sw' and not at all for 'breaksw'.

It helps the compiler check for mistakes if you mark your pin names as 'const', like:

const int brake=12;
const int brakesw=9;
const int twostep=7;
const int sw=A1;

Found the auto format, noted for later use someday!
Darn it, I was testing with LED as a label and changed them last night before bed, must have missed the brakesw.
Never heard of const int, will learn about that next.
I tried removing a '}' from if (analogRead(V1 line to see if fix the issue, clearly it didn't haha.

const makes the int value a constant. Once assigned it cannot/should not be changed. If there is an inadvertent attempt to do so, then the compiler will flag it.

AFAIK, the analogRead() method takes a single int value identifying the pin to be read*. However, V1 is a float calculated earlier in the loop. Not quite sure what was intended here? Were you trying to get the analog value of pin A1 (sw) again and compare or just trying to compare the previously calculated value of V1 to the upper and lower voltage limit?

Should it have been simply:

if (V1>=4.0 && V1<=4.5){
  do something ...
}
  • I stand to be corrected if some other notation that I am not aware of is permitted.

wildbill's example also shows clearly how statements should be placed within brackets within a nested if (if within an if) statement. Where you have multiple statements executed within an if .. else clause, each group of statements should be enclosed in curly brackets:

if (some condition)
{
  statement 1
  statement 2
  etc
}
else
{
  statement 1
  statement 2
  etc
}

Each nested if statement will have its own brackets to group the program statements within it:

if (some condition)
{
  statement 1;
  if (some other nested condition)
  {
     statement 1;
     statement 2;
     statement 3;
  }
  statement 2;
}
else
{
  statement 1;
  statement 2;
}

It can get somewhat confusing when there are if statements within if statements, but clear notation as shown in wildbill's example, will aid readability and make things clearer.

Ok I tried the changes said earlier, still having issues, will revisit later tonight when there are less distractions.
So with V1 I'm using a smart switch, at least that's what they are called at work. Its a three position switch and depending on the position it changes the voltage output. When its pressed up its activating the bump timer, when natural does nothing and when pressed down its activating a lock. And the lock is working fine, I've been leaving that part of the code out while testing this part.

Needs two analogReads.

if (analogRead() && analogRead()) {
 // do something
}

And the A/D of course returns an int, between 0 and 1023, assuming Uno.
Leo..

Thanks everyone! Got the nested if statement set up correctly and removed the analogRead from the V1 line and it works! Hopefully get in on a board tomorrow

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.