Demo entry 6361054

源代码

   

Submitted by anonymous on May 03, 2017 at 09:04
Language: C. Code size: 6.3 kB.

#include <math.h>        //包含数学库
//输入板子的管脚码
int pinNumLed1=3 ;
int pinNumLed2=7 ;
int pinNumLed3=9 ;
int pinNumLight=A1 ;
int pinNumTemperture=A0 ;

int Intensity = 0;//光照度数值

void setup() {
  // put your setup code here, to run once:
  pinMode(pinNumLed1, OUTPUT);
  pinMode(pinNumLed2, OUTPUT);
  pinMode(pinNumLed3, OUTPUT); 
  Serial.begin(9600);//设置波特率9600 
}

void fun1_1()
{
  digitalWrite(pinNumLed1, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinNumLed2, HIGH); 
  digitalWrite(pinNumLed3, HIGH); 
  }

void fun1_2()
{
  
  digitalWrite(pinNumLed1, LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinNumLed2, LOW); 
  digitalWrite(pinNumLed3, LOW); 
  
  }

  void fun2()
{
  digitalWrite(pinNumLed1, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinNumLed2, HIGH);
  digitalWrite(pinNumLed3, HIGH);
  delay(500);                       // wait for a second
  digitalWrite(pinNumLed1, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(pinNumLed2, LOW); 
  digitalWrite(pinNumLed3, LOW); 
  delay(500);                       // wait for a second
  
  }

  void fun3()
{
  // put your main code here, to run repeatedly:
  //如果按顺序连接,可以用for循环
  digitalWrite(pinNumLed1,HIGH);// turn the LED on (HIGH is the voltage level)
  delay(1000);// wait for a second
  digitalWrite(pinNumLed1,LOW);// turn the LED off by making the voltage LOW
  digitalWrite(pinNumLed2,HIGH);
  delay(1000);
  digitalWrite(pinNumLed2,LOW);
  digitalWrite(pinNumLed3,HIGH);
  delay(1000);
  digitalWrite(pinNumLed3,LOW);
  }

  void fun4()
{
  //如果按顺序连接,可以使用for循环
//pinNum1:
  // fade in from min to max in increments of 5 points:
  for(int fadeValue=0;fadeValue<=255;fadeValue+=5){
    //sets the value (range from 0 to 255)
    analogWrite(pinNumLed1,fadeValue);
    //wait for 30 milliseconds to see the dimming effect

    delay(30);
  }
  // fade out from max to min in increments of 5 points:
  for(int fadeValue=255;fadeValue>=0;fadeValue-=5){
    //sets the value (range from 255 to 0)
    analogWrite(pinNumLed1,fadeValue);
    //wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

//pinNum2:
  // fade in from min to max in increments of 5 points:
  for(int fadeValue=0;fadeValue<=255;fadeValue+=5){
    //sets the value (range from 0 to 255)
    analogWrite(pinNumLed2,fadeValue);
    //wait for 30 milliseconds to see the dimming effect

    delay(30);
  }
  // fade out from max to min in increments of 5 points:
  for(int fadeValue=255;fadeValue>=0;fadeValue-=5){
    //sets the value (range from 255 to 0)
    analogWrite(pinNumLed2,fadeValue);
    //wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

//pinNum3:
  // fade in from min to max in increments of 5 points:
  for(int fadeValue=0;fadeValue<=255;fadeValue+=5){
    //sets the value (range from 0 to 255)
    analogWrite(pinNumLed3,fadeValue);
    //wait for 30 milliseconds to see the dimming effect

    delay(30);
  }
  // fade out from max to min in increments of 5 points:
  for(int fadeValue=255;fadeValue>=0;fadeValue-=5){
    //sets the value (range from 255 to 0)
    analogWrite(pinNumLed3,fadeValue);
    //wait for 30 milliseconds to see the dimming effect
    delay(30);
  }
}

void funLight(){
  Intensity = analogRead(pinNumLight);  //读取模拟口AD5的值,存入Intensity变量  
  Serial.print("Intensity = ");  //串口输出"Intensity = "  
  Serial.println(Intensity);     //串口输出Intensity变量的值,并换行  
  delay(1000);           //延时1000ms 
  }

void funTemp(){
  int sensorValue = analogRead(A0);           //读取A0输入的电压量化值
   float voltage = sensorValue * (5.0 / 1023.0);  //将量化值还原成电压值
   //NTC换算公式:https://zh.wikipedia.org/wiki/%E7%83%AD%E6%95%8F%E7%94%B5%E9%98%BB
   const double B=3435000;
   const double R0=10000;
   const double T0=298.15;
   double r=R0*exp(-B/T0);
   double R=10000/(5-voltage)*voltage;//欧姆定律
   double temp=B/(log(R/r));
   temp=temp-273.15;
   Serial.println(temp);
   delay(1000);
  }


char input;
String comdata = "";

void loop() {

  while (Serial.available() > 0) {
    comdata += char(Serial.read());
    delay(2);
    if (comdata == "a") {
      digitalWrite(pinNumLed1, HIGH);   // turn the LED on (HIGH is the voltage level)
      digitalWrite(pinNumLed2, HIGH); 
      digitalWrite(pinNumLed3, HIGH);
    } else if (comdata == "b"){
        digitalWrite(pinNumLed1, LOW);   // turn the LED on (HIGH is the voltage level)
      digitalWrite(pinNumLed2, LOW); 
      digitalWrite(pinNumLed3, LOW);
    } else if (comdata[0] == 'l') {
      int data = 0;
      for (int i = 0; i < 3; ++i) {
        data = data * 10 + (comdata[i+1] - '0');
      }
      analogWrite(pinNumLed1,data);
    }else if (comdata[0] == 'm') {
      int data = 0;
      for (int i = 0; i < 3; ++i) {
        data = data * 10 + (comdata[i+1] - '0');
      }
      analogWrite(pinNumLed2,data);
    }else if (comdata[0] == 'n') {
      int data = 0;
      for (int i = 0; i < 3; ++i) {
        data = data * 10 + (comdata[i+1] - '0');
      }
      analogWrite(pinNumLed3,data);
    }
  }
  if (comdata.length() > 0) {
    Serial.println(comdata);
    comdata = "";
  }


//返回温度
  {
    int sensorValue = analogRead(pinNumTemperture);           //读取A0输入的电压量化值

//Serial.println(sensorValue);
  
   float voltage = sensorValue * (5.0 / 1023.0);  //将量化值还原成电压值

//Serial.println(voltage);
   
   //NTC换算公式:https://zh.wikipedia.org/wiki/%E7%83%AD%E6%95%8F%E7%94%B5%E9%98%BB
   const double B=3435000;
   const double R0=10000;
   const double T0=298.15;

//Serial.println(-B/T0);
//Serial.println(exp(-B/T0));
   
   double R=10000/(5-voltage)*voltage;//欧姆定律

//Serial.println(R);
   
   double temp=B/(log(R)-log(R0)+B/T0);

//Serial.println(temp);
   
   temp=temp-273.15;
   Serial.print("Temperature=");
   Serial.print(temp);
   Serial.println("°C");
   delay(1000);
    }

//返回光强
    {
   Intensity = analogRead(pinNumLight);  //读取模拟口AD5的值,存入Intensity变量  
   Serial.print("Intensity = ");  //串口输出"Intensity = "  
   Serial.println(Intensity);     //串口输出Intensity变量的值,并换行  
   delay(1000);           //延时1000ms 
      }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).