if and else clause help

Hello there,
Please be patient as I am very much a beginner. I've worked through quite a lot of Earthshine Electronic's beginners manual but I regret to say it was starting to go over my head... and I've scrutinised Banzi's 'getting started' booklet. That's what you're dealing with here :slight_smile:

I'm studying art part time. What I want to make is something that will wiggle a magnet underneath some ferrofluid when someone comes near it.

So I've got a mini servo. And I've worked out how to get that to move using the 'sweep' code on this website.

And I've got a sharps IR sensor. And I have used the 'analogreadserial' code to get a number from that.

I'm not trying to do anything clever, I just want the IR to trigger the movement of the servo if someone comes near, giving a reading between 100 and 300. (Sadly I'm not even sure how to do that, so I figured I'd start with it if it was just above 100.)

So I figured I could combine the two bits of code and that would be a start. But I really can't figure out the syntax so that it is acceptable.

Do I not need it to say 'if (the number from the IR is between 100 and 300) do the wiggling'
'else (wait for a bit)'??
Because then if it does trigger the wiggling, it'll just wiggle the once and then stop. And then it'll check again to see if anyone's still nearby.

This is what I've got so far. Can anyone tell me what the matter is? I'm sorry to have to ask but I don't know where else to go. I don't know how to get myself from utter beginner to remotely self-sufficient :frowning:
many thanks for any pointers,
Epona

/*this is taken from the analogreadserial code and the sweep code*/

#include <Servo.h>
 
Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position


void setup() {
  Serial.begin(9600);

myservo.attach(9);  // attaches the servo on pin 9 to the servo object}
}


void loop() {
  int sensorValue = analogRead(A0);

if (sensorValue >100);  

  {{for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }}
else 
{delay 10000} }}
if (sensorValue >100);

Your "if" does precisely nothing - remove the semicolon

However, delay is a function and need parentheses and a semicolon:

delay (10000);

The compiler doesn't care about indentation, but it is easier for humans to read if you remain consistent.

void loop() 
{
  int sensorValue = analogRead(A0);

  if (sensorValue >100) 
  {
     for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 179 degrees
     {                                  // in steps of 1 degree
        myservo.write(pos);              
        delay(15);                       
     }
     for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
     {                                
        myservo.write(pos); 
        delay(15);          
      }
  } 
  else 
  {
    delay (10000);
  }
}

Thank you SO much, AWOL, for your helpful reply. I will tinker some more. You see, it's not understanding the tiny little things like knowing where you do and do not want a semi colon that doesn't help my cause. Because I end up not knowing whether things are totally wrong or just need the smallest tweak.

I suppose all I can do is keep persevering!

But can you (or anyone) recommend me a book to learn from?

But apart from that I should just keep going shouldn't I. Everyone has to learn somehow, and trial and error (though slow) is probably a good way. And having a good reason to (ie a deadline and snarling tutors) should be good motivation :slight_smile:

I'm not sure about books when it comes to embedded, but my favourite book on C (with some C++ crossover) is Kelley and Pohl's "A Book on C".

Try some of the examples and projects in the tutorials and on the playground.

Everyone has to learn somehow, and trial and error (though slow) is probably a good way. And having a good reason to (ie a deadline and snarling tutors) should be good motivation :slight_smile:

Absolutely, trial and error is a good way, as long as the trying is methodical, and you learn what works and what doesn't. Random pecking of keys doesn't. Deadlines are good at keeping you focused, as long as the deadline is self imposed. Planning to complete assignments a week early is a self-imposed deadline. Posting on the forum for someone to do your homework for you, the night before it is due is not the kind of pressure you want to endure.

I don't know about the snarling tutors. Every interaction I've ever had with an instructor has been positive. I've found that teachers are really people, first. If you can see them as people, first, and instructors (authority figures) second, you just might find a way to get along with them. One of the best ways I've found is "Hey, teach, I've read the material, I've tried to do the assignment, but, something isn't clicking. Can we talk about it?". Of course, this needs to be done BEFORE the assignment is due.

Thanks for the book tip AWOL. And Paul, I totally take your point. You'll be pleased to hear though that I'm 37, my deadline's in June, and I don't get homework! I'm doing an MA and it's very much down to me what I do. Everyone else seems to be doing screenprinting but as usual I have to be awkward and do something rather different! The tutors can be a bit detached really, I suppose they think it's Good For Us. I thought the arduino teacher was great in the workshop I went to but he's very hard to track down outside such things. But never mind.

I've been quite methodical with writing down everything I've been trying (even though it can be much longer winded and the temptation is to just change things quickly). I will keep at it, and thanks for the encouragement.

I'm not saying I won't be back though :slight_smile: