极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 4548|回复: 1

求助,新手arduino代码問題

[复制链接]
发表于 2015-2-19 13:33:31 | 显示全部楼层 |阅读模式
借用了czad大大的Timer, 想同時控制fan和上传DHT11数据
但一直上传不了想請教各位大大帮助

#include "Timers.h"
#include <SPI.h>
#include <Ethernet.h>
#include <LeweiTcpClient.h>
#include <EEPROM.h>
#include <dht11.h>

#define LW_USERKEY "073193205b564a9b90207133a9a65be4"
#define LW_GATEWAY "01"

dht11 DHT11;
//DHT11 vcc pin->+5v
//DHT11 data pin->d2
//DHT11 gnd pin->gnd
#define DHT11PIN 2

//byte mac[] = {0x74,0x69,0x69,0x2D,0x30,0x31};
//IPAddress ip(192,168,1, 15);
//IPAddress mydns(8,8,8,8);
//IPAddress gw(192,168,1,1);
//IPAddress subnet(255,255,255,0);
LeweiTcpClient *client;

long starttime;
int postInterval = 10000;

#define HG7881_B_IA 10 // D10 --> Motor B Input A --> MOTOR B +
#define HG7881_B_IB 11 // D11 --> Motor B Input B --> MOTOR B -

// functional connections
#define MOTOR_B_PWM HG7881_B_IA // Motor B PWM Speed
#define MOTOR_B_DIR HG7881_B_IB // Motor B Direction

// the actual values for "fast" and "slow" depend on the motor
#define PWM_SLOW 50  // arbitrary slow speed PWM duty cycle
#define PWM_FAST 200 // arbitrary fast speed PWM duty cycle
#define DIR_DELAY 1000 // brief delay for abrupt motor changes



void setup()
{
  Serial.begin(9600);

  client = new LeweiTcpClient(LW_USERKEY, LW_GATEWAY);
  
  pinMode( MOTOR_B_DIR, OUTPUT );
  pinMode( MOTOR_B_PWM, OUTPUT );
  digitalWrite( MOTOR_B_DIR, LOW );
  digitalWrite( MOTOR_B_PWM, LOW );
   
   starttime = millis();
   
  TCs.AddFunc(0, sendDht11Value, 200);
  TCs.AddFunc(1, fan, 300);
}

void loop()
{
   
}

void sendDht11Value()
{
    if ((millis()-starttime) > postInterval)
    {
  //read DHT11 sensor
      int chk = DHT11.read(DHT11PIN);
      switch (chk)
      {
        case DHTLIB_OK:
                    //Serial.print("*** start data collection ");
                    //Serial.println(DHT11.temperature);
                    //sending data
                    //you need to setup sensors named "temperature" and "humidity" on the web before you store the value.
                    client->appendSensorValue("temp",(String)DHT11.temperature);
                    //you can append more sensors here,and use sendSensorValue to send
                    client->sendSensorValue("hum",(String)DHT11.humidity);
                    break;
                    /*
        case DHTLIB_ERROR_CHECKSUM:
                    Serial.println("Checksum error");
                    break;
        case DHTLIB_ERROR_TIMEOUT:
                    Serial.println("Time out error,check your pin");
                    break;
        default:
                    Serial.println("Unknown error");
                    break;
                    */
    starttime = millis();  
    }
}
}

void fan()
{
  if(DHT11.temperature >=1)
digitalWrite( MOTOR_B_DIR, LOW );
        digitalWrite( MOTOR_B_PWM, LOW );
        delay( DIR_DELAY );
        
        digitalWrite( MOTOR_B_DIR, HIGH ); // direction = forward
        analogWrite( MOTOR_B_PWM, 255-PWM_FAST ); // PWM speed = fast
        
}
回复

使用道具 举报

发表于 2015-2-20 10:25:18 | 显示全部楼层
一次把幾樣東西夾在一起來問, 很難分辨問題.
Timer, DHT11, LeweiTcpClient (Ethernet?), 的程序也有可能出錯.
先把程式分拆開, 看看是那一部份出錯.  
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-17 17:07 , Processed in 0.062031 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表