Coding For Useless Box

Hello there,

i am a beginner at coding and wanted to create a useless box i keep seeing on youtube ( they keep saying its easy but none have how to code for it, YAY!)

i have my circuit set up and working i am just having hard time coming up with the proper code.

any help is appreciated!

Hi Ramiro

Did you create the circuit with circuito? If you did, could you include the website address of the circuit so that we can see which components you used?

I’ll help with the code if I can (depends on the components you’ve chosen).

Hope I can help

im not sure if this will pop up or not,

but i used two mico servos, two channel toggle switch, a arduino Nano.

Thank you for the reply!

1 Like

Yep, the link works:

When I get out of bed I’ll download the sample code provided with the project and use it as a starting point to make the code changes you need.

In the mean time, can you write down the behaviour you want from the code?

1 Like

I’ve created a github code repository (repo) so that you can see the code changes as they’re made. If there’s anything you don’t understand, please let me know and I’ll explain it. I’ve started the repo with the code that circuito produced so you should have the same and be able to follow along.

Given that you’re only going to be editing the one file, you might prefer to jump directly to

I’ve renamed the “Firmware” folder that circuito creates by default to “UselessBox”. (The folder/directory name must match the name of the “.ino” file.)

The first change I’ve made is to reduce the code to the smallest amount of code to test your circuit. If it works, the servos should be spun in order (servo 1 and then servo 2) and then it’ll output the state of the toggle switch on the serial interface which you should be able to see in the serial monitor.

Please let me know if this step works before we go into more detail.

1 Like


I will try it out when I get a chance!

So going over the code and adding the pins to the correct port, i run into the issue of just one servo just repeating constant and nothing else reading

Okay sorry, i do not wish to spam.

i can not get the switch to register or to move the servos and i have been trying to change some of the values for the servos to change how far they go, but i am running into a wall

No worries Ramiro, we’re going to write back and forth a lot to resolve this - it’s not spam.

How do you want the motors to behave in response to the switch? From the videos I’ve seen I only thought there was one motor to reset the switch. It looks like the “arm” that flips the switch also lifts the lid so there’s no need for a second motor.

Have you tried the latest version of the program on GitHub? And did you see from the serial output which way is true/false for the toggle switch? We’ll need that info for the code and build to know when to react to the switch being either true or false.

I’ve just updated the code to move servo 1 back and forth whenever it detects the switch is true (in one of the two positions).

Could you tell me which pins you’re using (I’ll update the code to save you having to correct it every time).

Something worth mentioning here is that the code currently reads the position of the switch as often as it possibly can. I’m not sure if digital pins read at the same speed as analogRead, but if anything it’ll be faster and analogRead

On ATmega based boards (UNO, Nano, Mini, Mega), it takes about 100 microseconds (0.0001 s) to read an analog input, so the maximum reading rate is about 10,000 times a second.

You can imagine this is unnecessary use of battery power so it would be worthwhile adding a delay in the loop but we can get into refinements once it’s working something like you want.

Hey bhofman,
I’m actually trying to copy this video

Pretty much I’m trying to get one servo to lift the lid so that the second servo will hit the switch ,

And I’m sorry but I don’t understand the part of the serial output for the switch , I did test it with my multi meter to see if it was good and I found out it was bad ( it had 6 pins and none of them gave me a reading ) so I ordered one with only 3 ,

I have attached switch to D4 , servo 1 to D3 and servo 2 to D2 ( Ps when I looked over the or original code on github you had Servo_1 for both loops)

That’s a COOL monkey! :smiley:

I’ve made an update now to use the pins you’ve chosen, and to move both motors. I’ve not put a delay in yet because that’s optimisation which can be done when you’ve got the code doing what you want it to.

To mimic the monkey in the video you shared, you’ll need to add delays in different places to control when the head pops up and when the arm moves. There’s also some cute animation in the arm and different speeds that the head rises at. These will be variations of the number used in the write methods for the servos (control the speed) and in the delays to control how long they remain active for.

Hey there sorry for the late reply.

i am having issues with the void setup () void look ()

it gives me the error that i need to define it

Okay please ignore the top one, apparently how i opened the file it loaded everything and it didnt want to read it right

okay so i hooked everything up and it all works!!

now i just need to change the coding alittle

i need Servo 1 to open lid, Hold for Servo 2 to flick the switch, Then Servo 1 to return to close the lid.

how can i do that?

1 Like

Excellent news! :smiley:

The method/function that controls servo 1 has to be split into methods to move it up, and then down again and move servo 1 between these two methods. Hopefully the comparison of before and after code will clarify that: