Hello friends,
i am new at arduino and i just tried to control 2 dc motors by an arduino nano. first motor go forward and reverse, second is only forward. and i have a potansiometer to set motor speeds.
i used 9 and 10 pins to use pwm and my code is like below.
my first question is that why the code words so slow about input signals, i read from serial port the changings and i can see changing pot level exact time when i changed it. but i take 5V from arduino pins and thouched to inputs to get signal when i changed ths signal it takes time maybe 10 second or more, and also my input signals changes when i do notinhg and watching. i thought it is because parazites but i dont know how to solve it.
isnt it right to use 5V on arduino to make Input signals.
and can you check my code because my project doesnt works correct motors doesnt works fine.
Thanks.
//motor A connected between A01 and A02
//motor B connected between B01 and B02
bool ileriButton = false;
bool geriButton = false;
bool kaziButton = false;
int STBY = 8; //standby
int pwmValue = 0;
//Kontrol
int analogPin = 3;
int val = 0;
int ileri = 12;
int geri = 2;
int kazi = 3;
//Motor A
int PWMA = 9; //Speed control
int AIN1 = 4; //Direction
int AIN2 = 5; //Direction
//Motor B
int PWMB = 10; //Speed control
int BIN1 = 6; //Direction
int BIN2 = 7; //Direction
void setup(){
TCCR1B = TCCR1B & 0b11111000;
pinMode(ileri, INPUT);
pinMode(geri, INPUT);
pinMode(kazi, INPUT);
pinMode(STBY, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
Serial.begin(9600);
}
void loop(){
ileriButton = digitalRead(ileri);
geriButton = digitalRead(geri);
kaziButton = digitalRead(kazi);
val = analogRead(analogPin); // read the input pin
if (ileriButton == HIGH){
move(1, val/4, 1); //motor 1, full speed, left
Serial.print("Motor 1 ileri, Hız: ");
Serial.print(val/4);
Serial.println();
delay(200);
}
if (geriButton == HIGH){
move(1, val/4, 0); //motor 1, full speed, left
Serial.print("Motor 1 geri, Hız: ");
Serial.print(val/4);
Serial.println();
delay(200);
}
if (kaziButton == HIGH){
move(2, val/4, 1); //motor 1, full speed, left
Serial.print("Motor 2 kazıma, Hız: ");
Serial.print(val/4);
Serial.println();
delay(200);
}
if (ileriButton == LOW && geriButton == LOW && kaziButton == LOW){
stop();
Serial.print("Motorlar duruyor ");
Serial.println();
delay(200);
}
}
void move(int motor, int speed, int direction){
//Move specific motor at speed and direction
//motor: 0 for B 1 for A
//speed: 0 is off, and 255 is full speed
//direction: 0 clockwise, 1 counter-clockwise
digitalWrite(STBY, HIGH); //disable standby
boolean inPin1 = LOW;
boolean inPin2 = HIGH;
if(direction == 1){
inPin1 = HIGH;
inPin2 = LOW;
}
if(motor == 1){
digitalWrite(AIN1, inPin1);
digitalWrite(AIN2, inPin2);
analogWrite(PWMA, speed);
}else{
digitalWrite(BIN1, inPin1);
digitalWrite(BIN2, inPin2);
analogWrite(PWMB, speed);
}
}
void stop(){
//enable standby
digitalWrite(STBY, LOW);
}
