用arduino和photosens移动马达

2024-09-30 22:17:07 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我开始使用我最近买的Arduino套件,并一直在尝试在我进入更复杂的事情之前进行马达移动(目前)

我未来小项目的重点是让Arduino在晚上从我的窗户附近感知光线。从那里它将有希望转动一个马达,击中我的闹钟。虽然现在我只想让马达在看到光的时候移动,当它停止看到光的时候关闭,因为我可以在几秒钟后自动关闭

以下是当前代码:

const int motorPin = 9;
const int sensorPin = 10;
int lightLevel, high = 0, low = 1023;

void setup()
{
  // Set up the motor pin to be an output:
  pinMode(motorPin, OUTPUT);

  // Set up the serial port:
  Serial.begin(9600);
}
void loop()
{
  motormoveLevel = analogRead(sensorPin);
  manualTune(); 
  analogWrite(motorPin, lightLevel);
}    


void manualTune()
{
  lightLevel = map(lightLevel, 0, 1023, 0, 255);
  lightLevel = constrain(lightLevel, 0, 255);
} 

它不会编译,但是我从中得到的代码是这样的,这里有一个可以打开电机几秒钟,然后断断续续地关闭它:

const int motorPin = 9;

void setup()
{
  // Set up the motor pin to be an output:
  pinMode(motorPin, OUTPUT);

  // Set up the serial port:
  Serial.begin(9600);
}

void loop()
{
   motorOnThenOff();
}


// This function turns the motor on and off like the blinking LED.
// Try different values to affect the timing.
void motorOnThenOff()
{
  int onTime = 3000;  // milliseconds to turn the motor on
  int offTime = 3000; // milliseconds to turn the motor off

  digitalWrite(motorPin, HIGH); // turn the motor on (full speed)
  delay(onTime);                // delay for onTime milliseconds
  digitalWrite(motorPin, LOW);  // turn the motor off
  delay(offTime);               // delay for offTime milliseconds
}

这个代码根据光电传感器打开和关闭led:

 const int sensorPin = 0;
 const int ledPin = 9;

 int lightLevel, high = 0, low = 1023;


void setup()
{
  pinMode(ledPin, OUTPUT);
}


void loop()
{
  lightLevel = analogRead(sensorPin);
  manualTune(); 
  analogWrite(ledPin, lightLevel);
}


void manualTune()
{
  lightLevel = map(lightLevel, 0, 1023, 0, 255);
  lightLevel = constrain(lightLevel, 0, 255);
} 

所以基本上,我试着用这两段代码让马达根据它是否感觉到光而运动。我的“弗兰肯斯坦怪兽”没有编译,因此,我想帮助梳理这两个代码,使电机移动时,光线击中光电传感器,而不是移动时,它是覆盖(我已经知道如何连接它)


Tags: theto代码turnintupsetconst