|
回復 1# a125125460
這裡有問題
for(i=0;i<4;i++)
{
sensorValue = Serial.read ();//將接受到的可變電阻值放到 sensorValue(我認為*)//
}
should be ----->sensorValue = Serial.read ();//
換以下的程式試試:
發送端:
int potPin1=3;
int sv1,sensorValue1;
void setup()
{
Serial.begin(9600);
}
void loop()
{
sensorValue1 = analogRead(potPin1);//讀取可變電阻值//
sv1 = map(sensorValue1, 0, 1023, 0, 255);
Serial.println(sv1, BYTE);
delay(200);
}
接收端:
#include <stdlib.h>
int EN = 6 ;
int IN1 =5 ;
int IN2 = 7 ;
int i =0;
int sensor;
byte sensorValue;
int n;
void setup()
{
Serial.begin(9600);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
n=0;
sensor = 0;
}
void loop()
{
if (Serial.available() > 0) {
sensorValue = Serial.read ();//將接受到的可變電阻值放到 sensorValue(我認為*)//
if (int(sensorValue) > 130){
n=map(sensor,129,255,0,255);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
analogWrite(EN, n);
}
else if( int(sensorValue) <126)
{
n = map(sensor,127,0,0,255);
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
analogWrite(EN, n);
}
else
{
n = 0;
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
}
Serial.println(n);
delay(200);
}
}
|
|