美文网首页
【arduino实验记录22】电机与智能风扇

【arduino实验记录22】电机与智能风扇

作者: Geekero | 来源:发表于2020-09-17 08:42 被阅读0次

Arduino

/*************************************************
 * As explained above, the amount to times you press the button should change the the rotation speed of fan.
 * Pressing it once will cause it to rotate slowly,
 * while pressing it three times will cause it to rotate quickly, and pressing it four times will cause it stop.
 ************************************************/
 // constants won't change.They're used here to set pin numbers:
 const int buttonPin = 2;
 const int ledPin    = 13;
 const int motorIn1  = 9;
 const int motorIn2  = 10;

 int stat = 0;
 #define rank1  150
 #define rank2  200
 #define rank3  250
 
 //variables will change:
 int buttonState; // the current reading from the input pin
 int lastButtonState = LOW; // the previous reading from the input pin

 // the following variables are long's because the time, measured in miliseconds,
 // will quickly become a bigger number than can be stored in int.
 long lastDebounceTime = 0; // the last time the output pin was toggled
 long debounceDelay = 50; // the debounce time; increase if the output flickers

 /*********************************************/
 void setup()
 {
  //set the led, motors as OUTPUT, button as INPUT
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  pinMode(motorIn1, OUTPUT);
  pinMode(motorIn2, OUTPUT);
  Serial.begin(9600);
 }

 void loop()
 {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);
  if (reading != lastButtonState) // If the button state is different  from last time
  {
    lastDebounceTime = millis(); // reset the debouncing timer
  }

  if ((millis() - lastDebounceTime) > debounceDelay){
    if (reading != buttonState)
    {
      buttonState = reading; //  Store the state of button in buttonState
      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH)
      {
        digitalWrite(ledPin, HIGH); //turn on the LED
        stat ++;
        if (stat >= 4) 
        {
          stat = 0;
        }
      }
      else
          digitalWrite(ledPin ,LOW);
    }
  }
  switch(stat)
  {
    case 1:
        clockwise(rank1);
        break;
    case 2:
        clockwise(rank2);
        break;
    case 3:
        clockwise(rank3);
        break;
    default:
        clockwise(0);
  }
  // save the reading. Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
 }

 /**************************************************/
 void clockwise(int Speed)
 {
  analogWrite(motorIn1, 0);
  analogWrite(motorIn2, Speed);
 }
 /*********************************************/

改造成·智能风扇

貌似有点小bug, 但主题功能实现

/*************************************************
 * As explained above, the amount to times you press the button should change the the rotation speed of fan.
 * Pressing it once will cause it to rotate slowly,
 * while pressing it three times will cause it to rotate quickly, and pressing it four times will cause it stop.
 ************************************************/
 // constants won't change.They're used here to set pin numbers:
 #include <OneWire.h> 
 #include <DallasTemperature.h>
 #include <LiquidCrystal_I2C.h>
 #include <Wire.h> 
 LiquidCrystal_I2C lcd(0x27, 16, 2); //set the LCD address to 0x27 for a 16 chars and 2 line display
 #define ONE_WIRE_BUS  7 //ds18b20 module attach to pin7
 //Setup a oneWire instance to communicate with any OneWire devices (not just Maxi/Dallas temperature ICs)
 OneWire oneWire(ONE_WIRE_BUS);
 //Pass our oneWire reference to Dallas Temperature.
 DallasTemperature sensors(&oneWire);

 const int buttonPin = 2;
 const int ledPin    = 13;
 const int motorIn1  = 9;
 const int motorIn2  = 10;

 int stat = 0;
 int tmp = 0;
 int temp = 0;
 #define rank1  150
 #define rank2  200
 #define rank3  250
 
 //variables will change:
 int buttonState; // the current reading from the input pin
 int lastButtonState = LOW; // the previous reading from the input pin

 // the following variables are long's because the time, measured in miliseconds,
 // will quickly become a bigger number than can be stored in int.
 long lastDebounceTime = 0; // the last time the output pin was toggled
 long debounceDelay = 50; // the debounce time; increase if the output flickers

 /*********************************************/
 void setup()
 {
  sensors.begin(); //initialize the bus
  lcd.init(); //initialize the lcd
  lcd.backlight(); //turn on the backlight
  //set the led, motors as OUTPUT, button as INPUT
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  pinMode(motorIn1, OUTPUT);
  pinMode(motorIn2, OUTPUT);
  Serial.begin(9600);
 }

 void loop()
 { 
  
  //call sensors.requestTemperatures() to issue a global temperature
  //request to all devices on the bus
  //Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); //send the command to get temperatures
  lcd.setCursor(0, 0); 
  lcd.print("TemC: ");
  lcd.print(sensors.getTempCByIndex(0)); //print the temperature on lcd1602
  lcd.print(char(223)); //print the unit "C"
  lcd.print("C");

  temp = sensors.getTempCByIndex(0);
  if (temp < 25) stat = 0;
  if (temp < 27 && temp >= 25) stat = 1;
  if (temp < 29 && temp >= 27) stat = 2;
  if (temp > 29) stat = 3;

  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);
  if (reading != lastButtonState) // If the button state is different  from last time
  {
    lastDebounceTime = millis(); // reset the debouncing timer
  }

  if ((millis() - lastDebounceTime) > debounceDelay){
    if (reading != buttonState)
    {
      buttonState = reading; //  Store the state of button in buttonState
      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH)
      {
        digitalWrite(ledPin, HIGH); //turn on the LED
        tmp ++;
        if (tmp >= 2) 
        {
          tmp = 0;
        }
      }
      else
          digitalWrite(ledPin ,LOW);
    }
  }
  switch(stat)
  {
    case 1:
        clockwise(rank1);
        break;
    case 2:
        clockwise(rank2);
        break;
    case 3:
        clockwise(rank3);
        break;
    default:
        clockwise(0);
  }
  // save the reading. Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
 }

 /******************************************************/
 void clockwise(int Speed)
 {
  analogWrite(motorIn1, 0);
  analogWrite(motorIn2, Speed);
 }
 /****************************************************/

相关文章

网友评论

      本文标题:【arduino实验记录22】电机与智能风扇

      本文链接:https://www.haomeiwen.com/subject/xtthyktx.html