电压测量

/*
 作者:极客工坊-迷你强
 时间:2012年6月19日
 IDE版本号:1.0.1
 发布地址:[url=http://www.geek-workshop.com]www.geek-workshop.com[/url]
 作用:介绍使用arduino模拟口测量电压,作为0-5V电压表使用实验
 
*/
 
float temp;   //创建一个浮点型变量temp作为存储空间准备存放数据
void setup()
{
   pinMode(2,OUTPUT);//设定数字IO口的模式,OUTPUT 为输出
  Serial.begin(9600);     //使用9600的波特率进行串口通讯
}
void loop()
{
 
  int V1 = analogRead(1);                    
//从A0口读取电压数据存入刚刚创建整数型变量V1,模拟口的电压测量范围为0-5V 返回的值为0-1024
  float vol = V1*(5.0 / 1023.0);               
//我们将 V1的值换算成实际电压值存入浮点型变量 vol
  if (vol == temp)                             
//这部分的判断是用来过滤重复的数据,只有本次的电压值和上次不一时才进行输出
  {
    temp = vol;                               //比较完成后,将这次的值存入比对比用的变量temp
  }
  else
  { 
    Serial.print(vol);                       //串口输出电压值,并且不换行  
    Serial.println(" V");                    //串口输出字符V,并且换行
    temp = vol;
      if(vol>1){
        digitalWrite(2,HIGH); //设定PIN5脚为LOW = 0V
  }else{
          digitalWrite(2,LOW); //设定PIN5脚为LOW = 0V
  }
    delay(1000);                           //输出完成后等待1秒钟,用于控制数据的刷新速度。
  }
  

}


#include 
#include 
#include 
int valread;
     String clientMsg ="";
         String flag1 ="N";
          int ledPin1=2; //设定控制LED的数字IO脚
          
          String flag2 ="N";
          int ledPin2=3; //设定控制LED的数字IO脚
          
          String flag3 ="N";
          int ledPin3=4; //设定控制LED的数字IO脚
          
           String flag4 ="N";
          int ledPin4=5; //设定控制LED的数字IO脚
          
           String flag5 ="N";
          int ledPin5=6; //设定控制LED的数字IO脚
          
           String flag6 ="N";
          int ledPin6=7; //设定控制LED的数字IO脚
          EthernetClient client;
// Enter a MAC address, IP address and Portnumber for your Server below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x02, 0xAA, 0xBB, 0xCC, 0xDA, 0x02 };
IPAddress serverIP(172,25,60,110);


int serverPort=56788;
 
// Initialize the Ethernet server library
// with the IP address and port you want to use
EthernetServer server(serverPort);
 
void setup()
{
   pinMode(ledPin1,OUTPUT);//设定数字IO口的模式,OUTPUT 为输出
   pinMode(ledPin2,OUTPUT);//设定数字IO口的模式,OUTPUT 为输出
   pinMode(ledPin3,OUTPUT);//设定数字IO口的模式,OUTPUT 为输出
   pinMode(ledPin4,OUTPUT);//设定数字IO口的模式,OUTPUT 为输出
   pinMode(ledPin5,OUTPUT);//设定数字IO口的模式,OUTPUT 为输出
   pinMode(ledPin6,OUTPUT);//设定数字IO口的模式,OUTPUT 为输出

  // start the serial for debugging
  Serial.begin(9600);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, serverIP);
  server.begin();
  Serial.println("Server started");//log
}
 
void loop()
{
  // listen for incoming clients
 client = server.available();
  if (client) {

    if (client.connected()) {
      while (client.available()) {
        char c = client.read();
        //Serial.print(c);
        clientMsg+=c;//store the recieved chracters in a string
        //if the character is an "end of line" the whole message is recieved
        if (c == '\n') {
       if(clientMsg.indexOf('1')>-1){
//Serial.println("test123");//log
      digitalWrite(ledPin1,HIGH); //设定PIN5脚为HIGH = 5V左右
         }else if(clientMsg.indexOf('A')>-1){      
    digitalWrite(ledPin1,LOW); //设定PIN5脚为LOW = 0V
         }
     
           if(clientMsg.indexOf('2')>-1){
//Serial.println("test123");//log
         digitalWrite(ledPin2,HIGH); //设定PIN5脚为HIGH = 5V左右
         }else if(clientMsg.indexOf('B')>-1){      
     digitalWrite(ledPin2,LOW); //设定PIN5脚为LOW = 0V
         }
        
        
               if(clientMsg.indexOf('3')>-1){
//Serial.println("test123");//log
    digitalWrite(ledPin3,HIGH); //设定PIN5脚为HIGH = 5V左右
         }else if(clientMsg.indexOf('C')>-1){      
    digitalWrite(ledPin3,LOW); //设定PIN5脚为LOW = 0V
         }
        
        
               if(clientMsg.indexOf('4')>-1){
//Serial.println("test123");//log
    digitalWrite(ledPin4,HIGH); //设定PIN5脚为HIGH = 5V左右
         }else if(clientMsg.indexOf('D')>-1){      
    digitalWrite(ledPin4,LOW); //设定PIN5脚为LOW = 0V
         }
        
        
               if(clientMsg.indexOf('5')>-1){
//Serial.println("test123");//log
    digitalWrite(ledPin5,HIGH); //设定PIN5脚为HIGH = 5V左右
         }else if(clientMsg.indexOf('E')>-1){      
      digitalWrite(ledPin5,LOW); //设定PIN5脚为LOW = 0V
         }
        
        
               if(clientMsg.indexOf('6')>-1){
//Serial.println("test123");//log
    digitalWrite(ledPin6,HIGH); //设定PIN5脚为HIGH = 5V左右
         }else if(clientMsg.indexOf('F')>-1){      
      digitalWrite(ledPin6,LOW); //设定PIN5脚为LOW = 0V
         }
        
        


         
         
   clientMsg ="";
        }
      }

    }
    // give the Client time to receive the data
    delay(1);
    // close the connection:
    client.stop();

}
 valread=analogRead(1); 
 if(valread<150){
       digitalWrite(ledPin1,LOW); //设定PIN5脚为LOW = 0V
    Serial.println(valread);
 }

 delay(200); 
  
}



添加返回值

#include 
#include 
#include 
int valread;
     String clientMsg ="";
         String flag1 ="N";
          int ledPin1=2; //设定控制LED的数字IO脚
          
          String flag2 ="N";
          int ledPin2=3; //设定控制LED的数字IO脚
          
          String flag3 ="N";
          int ledPin3=4; //设定控制LED的数字IO脚
          
           String flag4 ="N";
          int ledPin4=5; //设定控制LED的数字IO脚
          
           String flag5 ="N";
          int ledPin5=6; //设定控制LED的数字IO脚
          
           String flag6 ="N";
          int ledPin6=7; //设定控制LED的数字IO脚
          EthernetClient client;
// Enter a MAC address, IP address and Portnumber for your Server below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x02, 0xAA, 0xBB, 0xCC, 0xDA, 0x02 };
IPAddress serverIP(172,25,60,110);


int serverPort=56788;
 
// Initialize the Ethernet server library
// with the IP address and port you want to use
EthernetServer server(serverPort);
 
void setup()
{
   pinMode(ledPin1,OUTPUT);//设定数字IO口的模式,OUTPUT 为输出
   pinMode(ledPin2,OUTPUT);//设定数字IO口的模式,OUTPUT 为输出
   pinMode(ledPin3,OUTPUT);//设定数字IO口的模式,OUTPUT 为输出
   pinMode(ledPin4,OUTPUT);//设定数字IO口的模式,OUTPUT 为输出
   pinMode(ledPin5,OUTPUT);//设定数字IO口的模式,OUTPUT 为输出
   pinMode(ledPin6,OUTPUT);//设定数字IO口的模式,OUTPUT 为输出

  // start the serial for debugging
  Serial.begin(9600);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, serverIP);
  server.begin();
  Serial.println("Server started");//log
}
 
void loop()
{
  // listen for incoming clients
 client = server.available();
  if (client) {

    if (client.connected()) {
      while (client.available()) {
        char c = client.read();
        //Serial.print(c);
        clientMsg+=c;//store the recieved chracters in a string
        //if the character is an "end of line" the whole message is recieved
        if (c == '\n') {
       if(clientMsg.indexOf('1')>-1){
//Serial.println("test123");//log

      digitalWrite(ledPin1,HIGH); //设定PIN5脚为HIGH = 5V左右
      client.print("line1 open");
         }else if(clientMsg.indexOf('A')>-1){      
    digitalWrite(ledPin1,LOW); //设定PIN5脚为LOW = 0V
          client.print("line1 close");
         }
     
           if(clientMsg.indexOf('2')>-1){
//Serial.println("test123");//log
         digitalWrite(ledPin2,HIGH); //设定PIN5脚为HIGH = 5V左右
         client.print("line2 open");
         }else if(clientMsg.indexOf('B')>-1){      
     digitalWrite(ledPin2,LOW); //设定PIN5脚为LOW = 0V
          client.print("line2 close");
         }
        
        
               if(clientMsg.indexOf('3')>-1){
//Serial.println("test123");//log
    digitalWrite(ledPin3,HIGH); //设定PIN5脚为HIGH = 5V左右
    client.print("line3 open");
         }else if(clientMsg.indexOf('C')>-1){      
    digitalWrite(ledPin3,LOW); //设定PIN5脚为LOW = 0V
         client.print("line3 close");
         }
        
        
               if(clientMsg.indexOf('4')>-1){
//Serial.println("test123");//log
    digitalWrite(ledPin4,HIGH); //设定PIN5脚为HIGH = 5V左右
    client.print("line4 open");
         }else if(clientMsg.indexOf('D')>-1){      
    digitalWrite(ledPin4,LOW); //设定PIN5脚为LOW = 0V
         client.print("line4 close");
         }
        
        
               if(clientMsg.indexOf('5')>-1){
//Serial.println("test123");//log
    digitalWrite(ledPin5,HIGH); //设定PIN5脚为HIGH = 5V左右
    client.print("line5 open");
         }else if(clientMsg.indexOf('E')>-1){      
      digitalWrite(ledPin5,LOW); //设定PIN5脚为LOW = 0V
           client.print("line5 close");
         }
        
        
               if(clientMsg.indexOf('6')>-1){
//Serial.println("test123");//log
    digitalWrite(ledPin6,HIGH); //设定PIN5脚为HIGH = 5V左右
    client.print("line6 open");
         }else if(clientMsg.indexOf('F')>-1){      
      digitalWrite(ledPin6,LOW); //设定PIN5脚为LOW = 0V
           client.print("line6 close");
         }
        
        


         
         
   clientMsg ="";
        }
      }

    }
    // give the Client time to receive the data
    delay(1);
    // close the connection:
    client.stop();

}
 valread=analogRead(1); 
 if(valread<150){
       digitalWrite(ledPin1,LOW); //设定PIN5脚为LOW = 0V
    Serial.println(valread);
 }

 delay(200); 
  
}


你可能感兴趣的:(Arduino)