I need help with code

Hello there,
I Have 4 arduino UNO's and 1 nano availiable but I am trying to put most of my project in one and I've never really worked with arduinos or code in general before

I have finally had the will power to begin this project of basically Freddy from fnaf and I got working Eyes using 6 Servos and a mouth using one servo (Also there is 1 button on the nose which doesn't properly work with my current code)

basically I want it to look as human like as possible with blinking and looking around after I did that I added the jaw and the nose button but that's where it went down hill. because you cant multitask with one singular arduino UNO (to my knowledge) it blinks looks around and repeat with an occasional "jumpscare" by which he opens his mouth and widens his eyes
here is the current code sorry by the way:

#include <Servo.h>

Servo servo_2;
Servo servo_4;
Servo servo_7;
Servo servo_8;
Servo servo_12;
Servo servo_13;
Servo servo_10;
double math_random_int(int a,int b) {
	if (a > b) {
		int c = a;
		a = b;
		b = c;
	}
	return (double)random(a,b+1);
}

int fnc_dynamic_digitalRead(int _pin){
	pinMode(_pin,INPUT);
	return digitalRead(_pin);
}

void setup()
{
  	servo_2.attach(2);
	servo_4.attach(4);
	servo_7.attach(7);
	servo_8.attach(8);
	servo_12.attach(12);
	servo_13.attach(13);
	servo_10.attach(10);
	pinMode(6, OUTPUT);

randomSeed(analogRead(0));

	servo_2.write(90);
	delay(1);
	servo_4.write(90);
	delay(1);
	servo_7.write(35);
	delay(1);
	servo_8.write(155);
	delay(1);
	servo_12.write(40);
	delay(1);
	servo_13.write(155);
	delay(1);
	servo_10.write(0);
	delay(1);
	digitalWrite(6, LOW);

}


void loop()
{

  	for (int count = 0; count < 1000000; count++) {
  		delay((math_random_int(2000, 10000)));
  		servo_2.write(90);
  		delay(1);
  		servo_4.write(90);
  		delay(1);
  		delay((math_random_int(2000, 10000)));
  		for (int count = 0; count < math_random_int(0, 4); count++) {
  			delay((math_random_int(400, 1500)));
  				servo_7.write(100);
  				delay(1);
  				servo_8.write(105);
  				delay(1);
  				servo_12.write(90);
  				delay(1);
  				servo_13.write(80);
  				delay(1);
  			delay((math_random_int(300, 400)));
  				servo_7.write(35);
  				delay(1);
  				servo_8.write(155);
  				delay(1);
  				servo_12.write(40);
  				delay(1);
  				servo_13.write(155);
  				delay(1);
  			delay(1000);
  		}
  		if ((math_random_int(1, 20) >= 12)) {
  			servo_10.write((math_random_int(0, 80)));
  			delay(1);
  			delay(1000);
  		}

  		delay((math_random_int(2000, 10000)));
  		for (int count = 0; count < math_random_int(1, 5); count++) {
  			delay((math_random_int(200, 3000)));
  				servo_2.write((math_random_int(40, 120)));
  				servo_4.write((math_random_int(40, 120)));
  			delay(1000);
  		}
  	}
  	if ((math_random_int(1, 20) >= 19)) {
  			servo_8.write(170);
  			delay(1);
  			servo_12.write(25);
  			delay(1);
  			servo_10.write(0);
  			delay(1);
  		delay(7000);
  			servo_8.write(155);
  			delay(1);
  			servo_12.write(40);
  			delay(1);
  			servo_10.write(180);
  			delay(1);
  	}

  	if ((fnc_dynamic_digitalRead(6) == true)) {
  			servo_7.write(100);
  			delay(1);
  			servo_8.write(105);
  			delay(1);
  			servo_12.write(90);
  			delay(1);
  			servo_13.write(80);
  			delay(1);
  			servo_10.write(180);
  			delay(1);
  		delay(5000);
  	}

}

Current problems i have:
Blinking and then looking around isn't exactly my imagination of this project
button not working (at least with that code)
I wanna add at least 2 or 3 more servos but i am willing to put them on a separate Arduino because head and torso rotation should definitely be separate from the Eyes n stuff

if you have any suggestions tips or solutions then please don't be shy because i really need help and if i should just use more arduinos for different parts is there any way to link them together without purchasing 5 different units

thanks in advance and sorry again for the code

ps. all of the pins correlate to:
Servos on 2 and 4 - Eye movement left right up down
Servos on 7,8,12 and 13 - Each Eyelid upper left right lower left right
Servo on 10 - Jaw movement
Button on 6 - on the nose (Should make an iconic boop sound in the future)

I would want to know how this is all powered , all those servos may over load and damage the Arduino.

if (you have little coding experience and took that code from elsewhere)
{
 then it’s worth doing a bit of simple coding first;
}
else
{ 
Adding print statements in your code can help you identify errors 
}

You can get arduino multitasking with non-blocking "blink without delay" concept. But your current code has a lot delays and weird blocking loops so I am afraid it can't works this way at all.

1 Like

the maximum value of the int type on the Uno is 32767

its all just powered with one arduino and the provided cable I think because they are not all running at the same time currently this isn't a problem but I have a separate power supply for the servos

could you show me an example? because i am willing to change the code

good to know that was just for testing purposes anyways

The "Blink without delay" is an standard example of Arduino IDE.
Also try to search the forum for this words

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

To make it easier for yourself so you don't have to remember which servo serves which purpose, you can use sensible names instead of numbers. E.g.

Servo servoLeftEye;
Servo servoRightEye;

etcetera
and everywhere use those names of the numbered variables.

Also name the pin for the button; e.g.

const uint8_t pinButton = 6;

And (again) everywhere where you want to do something with the button pin you use that name.

Why would you make a button pin an output?

What is the use of this long for-loop?

Hello r1sing

Welcome to the world's best Arduino forum ever.

In general - Arrays and structs are your friends.

Regardless of your project, you should start by creating an abstraction for controlling a servo.

A servo has:

  • a connection pin
  • a minimum angle
  • a maximum angle
  • a Speed in degrees per second
  • a timer

This information has to be summarised in a data structure so that a scaling of n=n+1 servos in arrray is possible. A member function within the data structure controls the servo.

Have a nice day and enjoy coding in C++.

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