我正在研究用重型电机控制器控制大功率电机,我能够使电机旋转,但我无法为我的压力传感器加入程序并确定电机的速度。我很确定我在电路板上正确连接了部件,所以我认为我的代码必须有一些错误,或者缺少某些东西。
我正在使用的压力传感器: 我正在使用的电机控制器: http://www.robotshop.com/en/arduino-compatible-mega-motor-shield-1a-5-28v.html#Useful
我的代码:
int motorValue=0;
int sensorValue=0;
int sensorPin=A0;
int EnablePin=8;
int PWMPin2=3;
void setup() {
//put your setup code here, to run once:
pinMode(EnablePin, OUTPUT);
//pinMode(sensorPin, OUTPUT);
Serial.begin(9600);
pinMode(PWMPin2, OUTPUT);
pinMode(sensorPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
sensorValue = analogRead(sensorPin);
//float voltage = sensorValue * (5.0/1023.0);
//Serial.println(voltage);
delay(50);
motorValue=map(sensorValue,0,1023,0,255);
//motorValue=sensorValue/4;
//analogWrite(motorPin,motorValue);
digitalWrite(EnablePin, HIGH);
analogWrite(PWMPin2, motorValue);
}
Edit: The sensor works by reading the voltage. Here's an actual
picture of how I wired it:
I used this wiring to print out the
voltage with just the Arduino and it worked, and I'm using it with
the same wiring but while having the motor shield on top, so that's
why I'm guessing that I must not be programming the sensor right
with the motor controller. I couldn't get the voltage readings when
I used this setup with the motor driver shield.