/*
控制 兩個馬達 控制馬達轉速 使用PWM 功能
*/
// constants won't change. They're used here to
// set pin numbers:
const int sensorPinL = 2; //
const int sensorPinR = 3; //
int pwmR = 0;
int pwmL = 0;
const int motorLin1 = 10; // 左馬達 電機控制
const int motorLin2= 11; // 左馬達 電機控制
const int motorRin1 = 12; // 右馬達控制
const int motorRin2= 13; // 左馬達控制
// variables will change:
int sensorStateL = 0; // 左偵測器
int sensorStateR = 0; // 右偵測器
void setup() {
pinMode(motorRin1, OUTPUT);
pinMode(motorRin2, OUTPUT);
pinMode(sensorPinL, INPUT);
pinMode(sensorPinR, INPUT);
pinMode(5,OUTPUT); // 控制左電機馬達轉速 PWM
pinMode(6,OUTPUT);
}
void loop(){
// read the state of the pushbutton value:
analogWrite(5,200); //PWM 左馬達
analogWrite(6,200); //PWM 右馬達
sensorStateL = digitalRead(sensorPinL);
sensorStateR = digitalRead(sensorPinR);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (sensorStateL == HIGH) {
// turn LED on:
digitalWrite(motorLin1, HIGH);
digitalWrite(motorLin2, LOW);
}
else {
// turn LED off:
digitalWrite(motorLin2, HIGH);
digitalWrite(motorLin1, LOW);
}
if (sensorStateR == HIGH) {
// turn LED on:
digitalWrite(motorRin1, HIGH);
digitalWrite(motorRin2, LOW);
}
else {
// turn LED off:
digitalWrite(motorRin2, HIGH);
digitalWrite(motorRin1, LOW);
}
}
------------------------------------------------------------------------------------我是分割線
// 首先定義腳位
#define led_pin 11
#define fsr_pin A0
void setup()
{
Serial.begin(115200);
pinMode(led_pin, OUTPUT);
}
void loop()
{
int fsr_value = analogRead(fsr_pin); // 讀取FSR
int led_value = map(fsr_value, 0, 1023, 0, 255); // 從0~1023映射到0~255
analogWrite(led_pin, led_value); // 改變LED亮度
Serial.println(fsr_value);
Serial.println(led_value);
Serial.println("-------------");
delay(100);
}
----------------------------------------------------------------------------------------------分割線
請問這兩個程式該怎麼結合會比較好呢???? |