I am using 4 motors with the arduino and I wrote up a simple code to control them by inputs from the keyboard 1,2,3,4
Whenever I click verify an error pops up on the bottom in orange color saying "error compiling" and that's it nothing else...
This is for an arduino duemilanove however I plan to make the final version on an atTiny2313
Here is the sketch
int MotorPin1 = 9;
int MotorPin2 = 10;
int MotorPin3 = 11;
int MotorPin4 = 12;
void setup()
{
Serial.begin(9600);
Serial.println("Robotic tank");
pinMode(MotorPin1, OUTPUT);
pinMode(MotorPin2, OUTPUT);
pinMode(MotorPin3, OUTPUT);
pinMode(MotorPin4, OUTPUT);
Serial.println("Wait for 1 second");
delay(1000);
Serial.println("You can now give commands");
}
void loop()
{
while (Serial.available() == 0);
int val = Serial.read() - '0';
if (val == 5)
{
Serial.println("Robot is On");
digitalWrite(MotorPin1, HIGH);
digitalWrite(MotorPin3, HIGH);
delay(500);
digitalWrite(MotorPin1, LOW);
digitalWrite(MotorPin3, LOW);
}
if (val == 1)
{
Serial.println("Robot is turning left");
digitalWrite(MotorPin1, HIGH);
digitalWrite(MotorPin4, HIGH);
delay(500);
digitalWrite(MotorPin1, LOW);
digitalWrite(MotorPin4, LOW);
}
if (val == 3)
{
Serial.println("Robot is turning right");
digitalWrite(MotorPin2, HIGH);
digitalWrite(MotorPin3, HIGH);
delay(500);
digitalWrite(MotorPin2, LOW);
digitalWrite(MotorPin3, LOW);
}
if (val == 2)
{
Serial.println("Robot is moving backward");
digitalWrite(MotorPin2, HIGH);
digitalWrite(MotorPin4, HIGH);
delay(500);
digitalWrite(MotorPin2, LOW);
digitalWrite(MotorPin4, LOW);
}
else
{
delay(5);
}
}
Could anyone else verify this code on their IDE and tell me if there are any mistakes either in the code,
Thanks
For the final version the size of the sketch is a little too large, is there any possible way to shorten the sketch so that I can use it with an Attiny2313.
Thanks
Your while statement will only be executed while there are no bytes in the serial buffer. You want to have something in the serial buffer before you read it.
I have run the code on an arduino duemilanove with the appropriate pins and it worked perfectly, I just needed to shorten the code for the attiny2313,
Thanks
Serial.println is expensive - you don't need any of it's cleverness to emit strings - use Serial.write instead. Use Serial.read()==-1 instead of Serial.available. It's not enough though. Maybe some port manipulation instead of digitalwrite, get rid of the serial commands and hit the UART directly. Look at what the compiler's producing and see if there's anything else you can get rid of.
All in all though, I think I'd use a slightly more expensive chip.
The memory size as the code currently is 2552kB but the attiny2313 only supports upto 2000 kB approx.
So I wanted to shorten the code, I would like to keep all the functionality if possible as there are only 4 keys and 4 actions.
@wildbill: I will be trying that now and I will tell u soon if it worked
When I used this with the arduino duemilanove I had no problems at all and I didnt even have to alter any code either.
Would connecting the TX, RX, Gnd and 5V pins of this with the attiny work and will it be able to read the inputs from serial monitor of the arduino as given in the code?
vishalapr:
Would connecting the TX, RX, Gnd and 5V pins of this with the attiny work and will it be able to read the inputs from serial monitor of the arduino as given in the code?
Well, assuming you could just wire in the Serial Bluetooth module and use it without having to add any coed, No.
Think about it. There is no difference whatsoever between using Serial.read() or Serial.wite() when attached directly to the PC or to the PC through Bouetooth. Did you think that you could magically get the data to and from the Serial pin s on the Bluetooth module without using all the same Serial stuff?
Is there some very compelling reason for your insistence on using the ATTiny2313? I have been looking at your code on and off since you posted it, and I don't see any way to reduce it to fit, even after eliminating all Seria output stuff, without sacrificing functionality. The best I could do was 2556 bytes.
kk, I changed over to an arduino mini, now I wired everything up and using the same code with different pin numbers.
When I turn it on, the motor on 3,4 of the H-bridge works fine as it should but the motor on 1,2 of H-bridge does not work as it starts running as soon as I turn it on without even pressing a single number on the computer. However when I press 2 or 3 on the computer it stops instead of running backwards and on pressing 1 or 5 it keeps running as it should and when I dont press anything it still keeps running instead of doing nothing. I am pretty sure I wired everything correctly following this :
int MotorPin1 = 3;
int LED1 = 10;
int LED2 = 11;
int LED3 = 12;
int MotorPin2 = 5;
int MotorPin3 = 6;
int MotorPin4 = 9;
void setup()
{
Serial.begin(9600);
pinMode(MotorPin1, OUTPUT);
pinMode(MotorPin2, OUTPUT);
pinMode(MotorPin3, OUTPUT);
pinMode(MotorPin4, OUTPUT);
Serial.println("Hello");
}
void loop()
{
while (Serial.available() == 0);
int val = Serial.read() - '0';
if (val == 5)
{
digitalWrite(LED1, HIGH);
Serial.println("Forward");
digitalWrite(MotorPin1, HIGH);
digitalWrite(MotorPin3, HIGH);
delay(500);
digitalWrite(LED1, LOW);
digitalWrite(MotorPin1, LOW);
digitalWrite(MotorPin3, LOW);
}
if (val == 1)
{
digitalWrite(LED2, HIGH);
Serial.println("Left");
digitalWrite(MotorPin1, HIGH);
digitalWrite(MotorPin4, HIGH);
delay(500);
digitalWrite(LED2, LOW);
digitalWrite(MotorPin1, LOW);
digitalWrite(MotorPin4, LOW);
}
if (val == 3)
{
digitalWrite(LED3, HIGH);
Serial.println("Right");
digitalWrite(MotorPin2, HIGH);
digitalWrite(MotorPin3, HIGH);
delay(500);
digitalWrite(LED3, LOW);
digitalWrite(MotorPin2, LOW);
digitalWrite(MotorPin3, LOW);
}
if (val == 2)
{
Serial.println("Backward");
digitalWrite(MotorPin2, HIGH);
digitalWrite(MotorPin4, HIGH);
delay(500);
digitalWrite(MotorPin2, LOW);
digitalWrite(MotorPin4, LOW);
}
else
{
delay(5);
}
}
The wiring of the h bridge is the same as in the video and I powered the motors and arduino and h bridge via a 5 volt power source. The bluetooth module is connected to TX, RX, 5v, and Gnd of the Arduino mini.
Personally, I think MotorPin1, MotorPin2, MotorPin3, and MotorPin4 are dumb names. The H-Bridge has pins that control the direction that the motors turn, and the speed that the motors turn. Using names like MotorADir, MotorASpeed, MotorBDir,and MotorBSpeed makes it far easier to determine that the correct pin is being set to the correct value. The direction pins do not need to be connected to PWM pins. Using PWM pins when you (seem to) have no intention of running the motors anything other than flat out or stopped is useless, too.
I can't tell from your names which pins control the direction of the motors and which control the speed.