What am I doing wrong?

Hi,
I bought an arduino mega 2560 and wired it so I have one servo and one motor. they both work but i am trying to write a program where i can control the motor with key 1 and the servo with keys 8,9,0. when i hold any button down it turns the servo one way. I had a motor shield but i took it off because it was complicating things. here is my code. could you please help. thanks.

#include <Servo.h>
int motorf = 9;
Servo myservo;
int pos = 0;
void setup() {
myservo.attach(7);
Serial.begin(9600);
pinMode(motorf, OUTPUT);
// put your setup code here, to run once:

}

void loop() {
while (Serial.available() == 0);
int val = Serial.read() - '0';
int i = myservo.read();
// put your main code here, to run repeatedly:
if (val == 1) ;
{
digitalWrite(motorf, HIGH);
delay(1);
digitalWrite(motorf, LOW);
delay(1);
}

if (val == 8) ;
{
for(pos = 0; pos += 1;);
myservo.write(pos);
delay(15);
}
if (val == 0) ;
{
for(pos = 0; pos -= 1;);
myservo.write(pos);
delay(15);
}
if (val == 9) ;
{

myservo.writeMicroseconds(1500);
delay(5);
}
}

Lose the semicolon. Same in later if statements too. Code tags would be nice too.

if (val == 1) ;
if (val == 0) ;

Bzzzt.

Don't forget the code tags.

for(pos = 0; pos += 1;);

This is meaningless.
A for loop needs a start, an end and an increment - there is no end in this.
Also the final semicolon means it does nothing anyway.

If you mean to increase the position by 1 every time the button is detected all you need is

pos += 1;

But you will also need code to prevent pos exceeding 180 or whatever max you want to enforce.

Holding down a PC key to cause repeated action on an Arduino would be much better done with a PC program (perhaps written in Python) which sends a start character when the key pressed and a stop character when the key is released.

...R

And if you put your code in code tags as I have done you won't have silly smileys in it.

thanks, I got the servo to turn and go back to pos 0 when i press 8,9,0 but it only turns one way. also i decided to add the motor shield after all and i adjusted my program to it. although the motor still won't go. its a seed studio motor shield 2.o, sorry for not adding code tags

#include <Servo.h>
int motorf = 9;
int motorb = 10;
Servo myservo;
int pos = 0;
void setup() {
myservo.attach(7);
Serial.begin(9600);
pinMode(motorf, OUTPUT);
pinMode(motorb, OUTPUT);
// put your setup code here, to run once:

}

void loop() {
while (Serial.available() == 0);
int val = Serial.read() - '0';
int i = myservo.read();
// put your main code here, to run repeatedly:
if (val == 1)
{
digitalWrite(motorf, HIGH);
delay(1);
digitalWrite(motorf, LOW);
delay(1);
}
if (val == 2)
{
digitalWrite(motorb, HIGH);
delay(1);
digitalWrite(motorb, LOW);
delay(1);
}

if (val == 8)
{
for(pos = 0; pos += 1;);
myservo.write(pos);
delay(5);
}
if (val == 0)
{
for(pos>= 0; pos-= 1;);
myservo.write(pos);
delay(5);
}
if (val == 9)
{

myservo.writeMicroseconds(1500);
delay(5);
}
}

Did you intend if (val == '1') etc?

Again, don't forget the code tags.

Scroll up - the character conversion is already handled:

int val = Serial.read() - '0';

OP still needs to heed Robin2's comments on the for statements though.

i changed what robin 2 told me to do and everything works( i took the motor shield off) exept it only turns left

Oops, my bad.
Over-scroll.

for(pos>= 0; pos-= 1;);

This is still stupid.

In Reply #3 I suggested using a PC program to send data to the Arduino when a PC key is pressed or released and I suggested that such a program could be written in Python. It could also be written in any other PC language.

...R