新手白 发表于 2018-11-6 21:24:06

新手,求助

单片机纯新手,网上自学,一周时间,连抄带试。
突发奇想,想做个双路温度控制器。结果发现自己水平不行,下边代码大部分抄来的。特来求助。

UNO的A2~A5脚是设定按钮,想用这四个按钮分别改变温度设定的值(varR_set1,varR_set2).应该是代码部分有问题,C语言不懂,网上查教程,看得迷迷糊糊。请各高手给我指点迷津。
/**
****************************************
* @file         Gizwits + ArduinoUnoR3 Library
* @brief      Datapoints handle , Gizwits Process
* @author       Gizwits
* @date         2017-08-01
* @version      V03030000
* @copyright    Gizwits
*
* @note         机智云.只为智能硬件而生
*               Gizwits Smart Cloudfor Smart Products
*               链接|增值ֵ|开放|中立|安全|自有|自由|生态
*               www.gizwits.com
*
****************************************/
#include "Gizwits.h"
#include <Wire.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>

int jdq1pin = 10;
int jdq2pin = 11;
int led1pin = 12;
int led2pin = 13;
int temup1 = A3;
int temdw1 = A2;
int temup2 = A5;
int temdw2 = A4;
int sensorValue1 = 0;
int sensorValue2 = 0;
unsigned long varR_set1 = 0;
unsigned long varR_set2 = 0;
float wendu1;
float wendu2;
// 初始化针脚
const int rs = 9, en = 8, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

Gizwits myGizwits;

#define   KEY1            6
#define   KEY2            7
#define   KEY1_SHORT_PRESS1
#define   KEY1_LONG_PRESS   2
#define   KEY2_SHORT_PRESS4
#define   KEY2_LONG_PRESS   8
#define   NO_KEY            0
#define   KEY_LONG_TIMER    3

void setup() {

Serial.begin(115200);

pinMode(led1pin, OUTPUT);
pinMode(led2pin, OUTPUT);
pinMode(jdq1pin, OUTPUT);
pinMode(jdq2pin, OUTPUT);
pinMode(temup1, INPUT_PULLUP);
pinMode(temdw1, INPUT_PULLUP);
pinMode(temup2, INPUT_PULLUP);
pinMode(temdw2, INPUT_PULLUP);
pinMode(KEY1, INPUT_PULLUP);
pinMode(KEY2, INPUT_PULLUP);

    myGizwits.begin();
    //设置LCD要显示的列数、行数,即2行16列
    lcd.begin(16, 2);

    //输出Hello World
    digitalWrite(led1pin, HIGH);
    lcd.print("Smart E-blanket");
    delay(3000);
    lcd.clear();
    digitalWrite(led1pin, LOW);
}



unsigned long Last_KeyTime = 0;

unsigned long gokit_time_s(void)
{
return millis() / 1000;
}

char gokit_key1down(void)
{
unsigned long keep_time = 0;
if (digitalRead(KEY1) == LOW)
{
    delay(100);
    if (digitalRead(KEY1) == LOW)
    {
      keep_time = gokit_time_s();
      while (digitalRead(KEY1) == LOW)
      {
      if ((gokit_time_s() - keep_time) > KEY_LONG_TIMER)
      {
          Last_KeyTime = gokit_time_s();
          return KEY1_LONG_PRESS;
      }
      } //until open the key

      if ((gokit_time_s() - Last_KeyTime) > KEY_LONG_TIMER)
      {
      return KEY1_SHORT_PRESS;
      }
      return 0;
    }
    return 0;
}
return 0;
}

char gokit_key2down(void)
{
unsigned long keep_time = 0;
if (digitalRead(KEY2) == LOW)
{
    delay(100);
    if (digitalRead(KEY2) == LOW)
    {
      keep_time = gokit_time_s();
      while (digitalRead(KEY2) == LOW) //until open the key
      {

      if ((gokit_time_s() - keep_time) > KEY_LONG_TIMER)
      {
          Last_KeyTime = gokit_time_s();
          return KEY2_LONG_PRESS;
      }
      }

      if ((gokit_time_s() - Last_KeyTime) > KEY_LONG_TIMER)
      {
      return KEY2_SHORT_PRESS;
      }
      return 0;
    }
    return 0;
}
return 0;
}

char gokit_keydown(void)
{
char ret = 0;
ret |= gokit_key2down();
ret |= gokit_key1down();
return ret;

}

/**
* KEY_Handle
* @param none
* @return none
*/
void KEY_Handle(void)
{
/*Press for over than 3 second is Long Press*/
switch (gokit_keydown())
{
    case KEY1_SHORT_PRESS:
      //lcd.clear();
      lcd.print("Production Test Mode ");
      delay(1000);
      myGizwits.setBindMode(WIFI_PRODUCTION_TEST);
      break;
    case KEY1_LONG_PRESS:
      //lcd.clear();
      lcd.print("Wifi Reset");
      delay(1000);
      myGizwits.setBindMode(WIFI_RESET_MODE);
      break;
    case KEY2_SHORT_PRESS:
   // lcd.clear();
      lcd.print("Soft AP mode");
      delay(1000);
      myGizwits.setBindMode(WIFI_SOFTAP_MODE);
      //Soft AP mode
      break;
    case KEY2_LONG_PRESS:
    //lcd.clear();
    lcd.print("AirLink mode");
    delay(1000);
      myGizwits.setBindMode(WIFI_AIRLINK_MODE);
      //AirLink mode
      break;
    default:
      break;
}
}
/**
* Serial Init , Gizwits Init
* @param none
* @return none
*/

// put your setup code here, to run once:




//mySerial.println("GoKit initOK \n");



void wifiStatusHandle()
{
// if(myGizwits.wifiHasBeenSet(WIFI_SOFTAP))
{
//   mySerial.println(F("WIFI_SOFTAP!"));
}

//if(myGizwits.wifiHasBeenSet(WIFI_AIRLINK))
{
//    mySerial.println(F("WIFI_AIRLINK!"));
}

//if(myGizwits.wifiHasBeenSet(WIFI_STATION))
{
//    mySerial.println(F("WIFI_STATION!"));
}

//if(myGizwits.wifiHasBeenSet(WIFI_CON_ROUTER))
{
//   mySerial.println(F("WIFI_CON_ROUTER!"));
}

//if(myGizwits.wifiHasBeenSet(WIFI_DISCON_ROUTER))
{
//    mySerial.println(F("WIFI_DISCON_ROUTER!"));
}

//if(myGizwits.wifiHasBeenSet(WIFI_CON_M2M))
{
//    mySerial.println(F("WIFI_CON_M2M!"));
}

//if(myGizwits.wifiHasBeenSet(WIFI_DISCON_M2M))
{
//    mySerial.println(F("WIFI_DISCON_M2M!"));
}
}
/**
* Arduino loop
* @param none
* @return none
*/
void loop() {

//Configure network
//if(XXX) //Trigger Condition
//myGizwits.setBindMode(0x02);//0x01:Enter AP Mode;0x02:Enter Airlink Mode
/*if ((digitalRead(temup1) == LOW)||(digitalRead(temdw1) == LOW)||(digitalRead(temup2) == LOW)||(digitalRead(temdw2) == LOW))
   {
    digitalWrite(led1pin, HIGH);
    delay(1000);
   // digitalWrite(led1pin, LOW);
   }*/
sensorValue1 = analogRead(A0);
sensorValue2 = analogRead(A1);
float wendu1 = -0.10581*sensorValue1+74.6374;
float varW_temp1 = wendu1;//Add Sensor Data Collection
myGizwits.write(VALUE_temp1, varW_temp1);
lcd.setCursor(0,0);
lcd.print(wendu1); //获取温度
//delay(500);
lcd.print((char)223); //显示o符号
lcd.print("C"); //显示字母C
//delay(500);
float wendu2 = -0.10581*sensorValue2+74.6374;
float varW_temp2 = wendu2;//Add Sensor Data Collection
myGizwits.write(VALUE_temp2, varW_temp2);
lcd.setCursor(9,0);
lcd.print(wendu2); //获取温度
//delay(500);
lcd.print((char)223); //显示o符号
lcd.print("C"); //显示字母C

bool varR_power = 0;
    if(myGizwits.hasBeenSet(EVENT_power))
{
    myGizwits.read(EVENT_power,&varR_power);//Address for storing data
    //mySerial.println(F("EVENT_power"));
    //mySerial.println(varR_power,DEC);

}
bool varR_jdq1 = 0;
if(myGizwits.hasBeenSet(EVENT_jdq1))
{
    myGizwits.read(EVENT_jdq1,&varR_jdq1);//Address for storing data
    //mySerial.println(F("EVENT_jdq1"));
    //mySerial.println(varR_jdq1,DEC);

}
bool varR_jdq2 = 0;
if(myGizwits.hasBeenSet(EVENT_jdq2))
{
    myGizwits.read(EVENT_jdq2,&varR_jdq2);//Address for storing data
    //mySerial.println(F("EVENT_jdq2"));
    //mySerial.println(varR_jdq2,DEC);

}

   KEY_Handle();//key handle , network configure
wifiStatusHandle();//WIFI Status Handle
myGizwits.process();



if(myGizwits.hasBeenSet(EVENT_set1))
{
    myGizwits.read(EVENT_set1,&varR_set1);//Address for storing data

lcd.setCursor(0,1);
lcd.print(varR_set1); //获取设置
//delay(500);
lcd.print((char)223); //显示o符号
lcd.print("C"); //显示字母C
//delay(500);

    //mySerial.println(F("EVENT_set1"));
    //mySerial.println(varR_set1,DEC);
}
if(wendu1<varR_set1)
{
    digitalWrite(jdq1pin,HIGH);
}
if(wendu1>varR_set1+1)
{
    digitalWrite(jdq1pin, LOW);
}

if(myGizwits.hasBeenSet(EVENT_set2))
{
    myGizwits.read(EVENT_set2,&varR_set2);//Address for storing data
lcd.setCursor(9,1);
lcd.print(varR_set2); //获取设置
//delay(500);
lcd.print((char)223); //显示o符号
lcd.print("C"); //显示字母C
//delay(500);
}

    if(wendu2<varR_set2)
{
    digitalWrite(jdq2pin,HIGH);
}
if(wendu2>varR_set2+1)
{
    digitalWrite(jdq2pin, LOW);
}
   /*if ((digitalRead(temup1) == HIGH)||(digitalRead(temdw1) == HIGH)||(digitalRead(temup2) == HIGH)||(digitalRead(temdw2) == HIGH))
{
   digitalWrite(led1pin, LOW);
}*/
    if(temup1 = LOW)
    {
      varR_set1++;
    }
    if(temdw1 = LOW)
    {
      varR_set1--;
    }
    if(temup2 = LOW)
    {
      varR_set2++;
    }
    if(temdw2 = LOW)
    {
      varR_set2--;
      }
   // delay(1000);
   // digitalWrite(led1pin, LOW);
   // mySerial.println(F("EVENT_set2"));
   // mySerial.println(varR_set2,DEC);
   //binary datapoint handle
}

页: [1]
查看完整版本: 新手,求助