ผมไปก็อปโค้ดมาจากเว็ปแล้วรันเออเร่ออะครับ
โค้ดตามนี้ครับ ช่วยแก้ให้หน่อยครับ
/*
DC Motor example
This program drives a DC motor.
The motor is attached to PWM pins 5 and 6 of the Arduino.
If the button attached to A1 is pressed the motor should rotate clockwise,
while if the button attached to A2 is pressed motor should rotate counterclockwise.
The velocity is controlled by a trimmer connected to A2
Author: Arturo Guadalupi <a.guadalupi@arduino.cc>
*/
void setup() {
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
Serial.begin(9600);
}
int val;
int temp;
void loop() {
if (digitalRead(A1) == LOW)
{
val = analogRead(A0);
clockwise();
}
if (digitalRead(A2) == LOW)
{
val = analogRead(A0);
counterclockwise();
}
if ((digitalRead(A1) != LOW) && (digitalRead(A2) != LOW))
motorStop();
}
void clockwise()
{
analogWrite(5, map(vel, 0, 1023, 0, 255));
analogWrite(6, 0);
}
void counterclockwise()
{
analogWrite(6, map(Vel, 0, 1023, 0, 255));
analogWrite(5, 0);
}
void motorStop()
{
analogWrite(6, 0);
analogWrite(5, 0);
delay(2);
}
ช่วยหน่อยครับผมใช้ Arduino UNO R3 ในการปรับทิศทางมอเตอร์ 2 ทิศทาง ใครพอจะรู้โค้ดว่าเขียนยังไงช่วยบอกผมหน่อยครับ
โค้ดตามนี้ครับ ช่วยแก้ให้หน่อยครับ
/*
DC Motor example
This program drives a DC motor.
The motor is attached to PWM pins 5 and 6 of the Arduino.
If the button attached to A1 is pressed the motor should rotate clockwise,
while if the button attached to A2 is pressed motor should rotate counterclockwise.
The velocity is controlled by a trimmer connected to A2
Author: Arturo Guadalupi <a.guadalupi@arduino.cc>
*/
void setup() {
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
Serial.begin(9600);
}
int val;
int temp;
void loop() {
if (digitalRead(A1) == LOW)
{
val = analogRead(A0);
clockwise();
}
if (digitalRead(A2) == LOW)
{
val = analogRead(A0);
counterclockwise();
}
if ((digitalRead(A1) != LOW) && (digitalRead(A2) != LOW))
motorStop();
}
void clockwise()
{
analogWrite(5, map(vel, 0, 1023, 0, 255));
analogWrite(6, 0);
}
void counterclockwise()
{
analogWrite(6, map(Vel, 0, 1023, 0, 255));
analogWrite(5, 0);
}
void motorStop()
{
analogWrite(6, 0);
analogWrite(5, 0);
delay(2);
}