Problem with code (Beginner)

Hello,
I’m complete beginner on Arduino Uno, and I also don’t have much coding experience. I have Arduino Uno, 4 servos, and Arduino Shield (PCB board, checked and works as it is suppose to)

What I’m trying to achieve with my code:

  • Have a pushbutton to start the program
  • Program has started
  • Choose one of the servos randomly, turn it 90 degrees (repeat the process total of 4 times)
  • Return all the servos at once to their starting positions
  • End program

I have one code which doesn’t exactly do what I’m trying to achieve.
Problems in my code:

  • Starts the program immediately after I put the power source in (pushbutton is ignored)
  • Good thing: Chooses one of the servos randomly, turns it 90 degrees. Bad Thing: (repeats the process only 3 times for some reason)
  • Doesn’t end the program (After the servos have returned to their original positions, the program runs again. It’s in never ending loop)

Here is the code:

#include <Servo.h>

Servo servo[4];
const byte servoPins = {11,10,9,6};
int pos = 0;
int sat1 = 0;
int switchPin = 8;

void setup () {
for (int j=0; j<=4; j++){ //pitää servot paikallaan
servo[j].write(pos);
}
for (int n = 0; n < 4; n++){ //määrittää servot pinneille
servo[n].attach(servoPins[n]);
}
randomSeed(analogRead(A0));
pinMode(switchPin, INPUT);
}

void loop() {
int servoArray = {0,1,2,3};
const int servoCount = 4;
for (int i=0; i < servoCount; i++)
{
int n = random(0, servoCount);
int temp = servoArray[n];
servoArray[n] = servoArray*;*
_ servoArray = temp;_
* }*

* if (digitalRead(switchPin) == HIGH) {*
* servo[servoArray[0]].write(90);*
* delay(1000);*
* servo[servoArray[1]].write(90);*
* delay(1000);*
* servo[servoArray[2]].write(90);*
* delay(1000);*
* servo[servoArray[3]].write(90);*
* delay(1000);*
* }*

* for (int k=0; k<=4; k++) {*
* servo[k].write(0);*
* }*
* }*
I understand that it might have several problems, and I appreciate all the help I can get!

Use the code tags </> button in the posting menu for showing us your sketch.

for (int j=0; j<=4; j++){ <= ?

When loop() finishes, it starts over again.

Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components.
Posting images: https://forum.arduino.cc/index.php?topic=519037.0

Please post your code with the code tags. First of all, you should declare your servo array's with a global const and not a const in loop():

#define SERVO_COUNT 4
Servo servo[SERVO_COUNT];
const byte servoPins[] = {11,10,9,6};

The next thing is that this code is defunct:

int temp = servoArray[n]; //Get element
servoArray[n] = servoArray; //Assign array to element in array = problem
servoArray = temp; //Assign element from array to array = problem

You should also remember that random() may return the same number twice, so you need a better way to mix the servo sequence.

Thank you for your replies,
Here is picture of the PCB which is attached to Arduino Uno.

The PCB was checked for errors today in school lab, pins and ground working as intended.

Unfortunately I don’t have the schematic file since it’s saved in a school computer. I can get my hands on it in monday.

Components in the PCB:
two 2x3 pinheads (GND, power, PIN)

one 5V regulator (IN, OUT, GND)
two capacitors (10uF)

one pushbutton
one resistor (1kOhm)

The soldering looks quite bad :frowning:

What does this do?
for (int j=0; j<=4; j++){

You can draw a schematic on paper then image it for us.

Yes it was first time soldering but the teacher said it's working as it should be, so I guess the board is not the issue here?

Since it's a group project I'm not sure what that part of the code stands for. The code was written by another person. I'm just trying to find information what we could do about the code. We are all beginners with all of this stuff.

Paahema:
Yes it was first time soldering but the teacher said it's working as it should be, so I guess the board is not the issue here?

Your code cannot work, so start fixing it :slight_smile:

for (int j=0; j<=4; j++){

There are 5 iterations here, you have 4 servos :frowning:

"I’m not sure what that part of the code stands for. The code was written by another person. "
Then we cannot help you :frowning:
If you want help, you need to be able to understand what’s happening and take responsibility for the work.

Make up a schematic and clean up the soldering.

Another mishab: In setup() you write to the servos before you attach them to their pins.

Thanks for the replies again. Seems like there is plenty of stuff that needs fixing, and not only the code. Probably better for me to focuse on the points you both are making, and come back when board and the code are in better condition. Thank you for your advices, we can close the thread for now :).

Danois90:
Another mishab: In setup() you write to the servos before you attach them to their pins.

That is not a problem. (sp."mishap")

TolpuddleSartre:
That is not a problem. (sp."mishap")

Mishap times two :wink:

Take a look at this tutorial

and restructure your code accordingly.

Your button has only two states ON / OFF and all you have to do is align your "actions" under each state.
The tutorial "controls" LED , so what - you control servos.

Once you have the correct program structure you can work on the details.

Also - get out of bad habit coding without having feedback.
Take a look at Serial and its usage in "scaffolding" .
In your case - aren't you interested what is the actual random number generated?
Using Serial in debugging process is not that memory "expensive" and can be deleted if so.
Good luck coding.