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;_ * }*
* 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!
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.
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.
"I'm not sure what that part of the code stands for. The code was written by another person. "
Then we cannot help you
If you want help, you need to be able to understand what’s happening and take responsibility for the work.
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 :).
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.