Not understanding my code

Hi guys,

For starters, I am new to this. I made this a while back by linking arduino and processing. and now I'm not sure what my codes do exactly. can someone help me by giving some comments on my code? :slight_smile:

this is my arduino code;

#include <Servo.h>
int servoPin = 9;
int ledPin1 = 12;
int counter = 0;
int ServoPosition = 0;
int randNumber;
Servo ServoOne;
bool hasbeensend = false;

void setup()
{
Serial.begin(9600);
ServoOne.attach(servoPin);
pinMode(ledPin1, OUTPUT);
randomSeed(analogRead(0));
}

void loop()
{
if(counter <15){
digitalWrite(ledPin1, LOW);
ServoPosition = random(0,180);
ServoOne.write(ServoPosition);
delay(500);
counter ++;
randomSeed(analogRead(0));
}

if(counter >=14){
if (hasbeensend == false){
randNumber = random(1, 5);
Serial.write(randNumber);
hasbeensend = true;
}
digitalWrite(ledPin1, HIGH);
}
}

I made this a while back by linking arduino and processing. and now I'm not sure what my codes do exactly. can someone help me by giving some comments on my code?

Have I got this right ?

You "made" that code but you don't know what it does.

Is that correct ?

KikiKate:
yeah, i made it a while back and i am pretty new at this. so i did not work with it for some time and now i am not sure what everything does

Have you studied each line of the program and thought about what it is likely to do?

Maybe write some notes beside each line of code.

...R

It might be a good start to run it and see what it does. It seems there's a servo and an LED involved.

So why don't you try that then add comments to the parts of the code you do understand. Then we may be able to help with any confusing bits.

Steve

KikiKate:
yeah, i made it a while back and i am pretty new at this. so i did not work with it for some time and now i am not sure what everything does

Then see this as a lesson why to add comments to your program :wink:

But now, just go through it line by line and check what to program does and why. That would be the same we would need to do :slight_smile: At least your variable names seem to be kind of logical.

It might make a led turn on, then turn off after a while. And a motor twitches. Why did you write it?

KikiKate:
I'm not sure what my codes do exactly

You should come work for us. Most of our developers are in the same boat.

I'm always looking at my old code and going: "Who wrote this crap?"

Count your { }. Learn to format.