简体   繁体   English

用arduino和光电传感器移动电机

[英]Moving a motor with an arduino and a photo-sensor

So I started using an Arduino kit I bought recently, and have been attempting to make a motor move (for now) before I go onto more complex things. 因此,我开始使用我最近购买的Arduino套件,并且在尝试更复杂的事情之前一直尝试(现在)移动电动机。

The point of my little project in the future will be for the Arduino to sense light from near my window at night. 将来我的小项目的重点是让Arduino在晚上感应到我窗户附近的光线。 From there it will hopefully turn a motor that hits my alarm clock. 希望从那里可以转动撞到我闹钟的电动机。 Though for now I simply want to get the motor moving when it sees light, and off once it stops seeing light, as I can add an automatic turn off after a few seconds later. 尽管现在我只是想让电动机在看到光时运动,而一旦它停止见光就关闭,因为我可以在几秒钟后添加自动关闭功能。

Here is the current code: 这是当前代码:

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);
} 

It does not compile, however the codes I derived it from do, here is one that turns on the motor for a few seconds then turns it off intermittently: 它不会编译,但是我从中获得代码,这是一种将电动机打开几秒钟然后间歇性关闭的代码:

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
}

And this code turns an led on and off based on the photosensor: 这段代码根据光电传感器打开和关闭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);
} 

So basically, I'm trying to use these two pieces of code to make the motor move based on whether it senses light. 因此,基本上,我正在尝试使用这两段代码根据电动机是否感应光来使电动机运动。 My 'Frankenstein's-monster' isn't compiling and as such, I would like help in combing the two codes to make the motor move when light hits the photosensor, and not move when it is covered (I already know how to wire it). 我的“弗兰肯斯坦怪兽”没有编译,因此,我想帮助组合这两个代码以使电机在光线照射到光电传感器时移动,而在被覆盖时不移动(我已经知道如何接线) 。

您无法在引脚0上进行AnalogRead。必须使用A0-A5(14-19)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM