Please help us with school project!

Please, be our hero and help us with a school project called 'explore tech' (for not so tech people :o )

Background:
Our idea is to build a box in plexiglass and connect it to an app, in which you as a user creates lists of what e.g. your children is supposed to do to get their saturday candy. It can be such as:

  • Clean room (x)
  • Walk dog (x) (<- box that both children and admin of the list checks when finish task)
    etc.
    When the children and the parents clicked the boxes for all tasks, the lock on the plexi box will open and the children will get their candy. So it is supposed to be used as an motivational box (for children, offices etc).

Now to our struggle:
We got the lock to move back and forwards 0-180 degrees and just spin. But we want it to stop in between. So we want one button for: Open box. And one for: Close box.

This is the code we used to just make it spin:

// Sweep
// by BARRAGAN http://barraganstudio.com
// This example code is in the public domain.

#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()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop()
{
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
}
}

We would highly appreciate some help! :slight_smile:

#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()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object

 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
  }
}
 
 
void loop()
{
 
}

We would highly appreciate some help!

With what? If you can make the servo move at all, surely you can read the comments in the code and see why it moves the way that it does. Change the for statements, to see what the various numbers mean. Use you powers of observation!

The state change detection example will show you how to read a switch and make something happen when it becomes pressed or when it becomes released. Having that something be moving the servo to position 1 or to position 2 is incredibly trivial.

We got the lock to move back and forwards 0-180 degrees and just spin

What type of servo is it and how is it powered ? How do you plan to power the system when it is its box ?

We would highly appreciate some help!

WE WOULD HIGHLY APPRECIATE YOU NOT POSTING THE SAME QUESTION IN DIFFERENT PARTS OF THE FORUM.

ANSWEAR:
What type of servo is it and how is it powered ? How do you plan to power the system when it is its box ?

We have a "Futaba S3003" (a server motor for max 4,1 kg). And a 9V battery.
We want it to connect to a bluetooth- so that we can press a lock button and a unlock button.
The bluetooth we are using is called HC06!

So we want help with changing our original code and adding If and else in to it. So that it connects to our bluetooth and unlocks with pressing the "Unlock bottom" in the app and locks when pressing "Lock bottom" in the app.

And a 9V battery.

Sorry for the questions, but what sort of battery ? If it is what I know as a PP3 then it will not be able to provide the required current for very long. That raises another problem. What will happen if the power fails whilst the box is locked ? How will you open it ?

Have you figured out how to make the servo move to a position other than 0 or 180 degrees ?

Thanks for the input! We will have a USB cable as a backup! :slight_smile:
And yes, we can change the position of the servo, but we need to make it stop in between! So we think we need a "if" and a "else" code...

We will have a USB cable as a backup!

That sounds sensible.

How are you getting on controlling the servo position ?

If we want to change the position we just change the number in the code between 0-360 degrees depending of what position we want.

If we want to change the position we just change the number in the code between 0-360 degrees depending of what position we want.

Close, but no cigar.