极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10366|回复: 2

新人求教关于arduino uno

[复制链接]
发表于 2015-11-27 22:17:44 | 显示全部楼层 |阅读模式
arduino uno今天才到手。手痒的先玩了下。

/*

02. 作者:极客工坊-迷你强

03. 时间:2012年6月19日

04. IDE版本号:1.0.1

05. 发布地址:www.geek-workshop.com

06. 作用:介绍使用arduino模拟口测量电压,作为0-5V电压表使用实验

07.

08.*/



float temp;   //创建一个浮点型变量temp作为存储空间准备存放数据

void setup()

{

  Serial.begin(9600);     //使用9600的波特率进行串口通讯

}

void loop()

{



int V1 = analogRead(A0);                    

//从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;

   delay(1000);                           //输出完成后等待1秒钟,用于控制数据的刷新速度。

}

}
我把源吗烧录进去后。测试5v以内的都正常。后来我想。如果测试5v以上的电瓶电压,就搭了一个10k和500的电阻分压。
可是串口监视器始终显示的是电阻分压后的电压,可是我知道。分压电压和实际电压差了20倍,问题是在源码中怎么加入这一句呢?
回复

使用道具 举报

发表于 2015-11-28 23:18:09 | 显示全部楼层
int V=analogRead(V0)*20;
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-12-10 18:02:54 | 显示全部楼层
林定祥 发表于 2015-11-28 23:18
int V=analogRead(V0)*20;

非常感谢答疑

现在又有个问题
原帖地址http://www.arduino.cn/thread-5615-1-1.html

/********************************************************
* PID RelayOutput Example
* Same as basic example, except that this time, the output
* is going to a digital pin which (we presume) is controlling
* a relay.  the pid is designed to Output an analog value,
* but the relay can only be On/Off.
*
*   to connect them together we use "time proportioning
* control"  it's essentially a really slow version of PWM.
* first we decide on a window size (5000mS say.) we then
* set the pid to adjust its output between 0 and that window
* size.  lastly, we add some logic that translates the PID
* output into "Relay On Time" with the remainder of the
* window being "Relay Off Time"

PID继电器输出范例
与基本范例相同,这一次输出是一个数字引脚控制的继电器。PID被设计成
输出一个模拟值,但是继电器只有开关状态。
为了联系上两者,我们使用时间比例控制,它本质上是一个很慢的PWM。
首先我们决定一个窗口时间(比如5000ms)。
然后设置PID适应它的输出在0到窗口时间的范围。
最后我们添加一些逻辑,把PID输出转换成“继电器接通时间”和剩余的
“继电器断开时间”
********************************************************/

#include <PID_v1.h>
#define RelayPin 8
// 定义我们将要使用的变量
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//指定链接和最初的调优参数
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);

int WindowSize = 2000;
unsigned long windowStartTime;
void setup()
{
  windowStartTime = millis();
  //初始化变量
  //initialize the variables we're linked to
  Setpoint = 100;
  //告诉PID在从0到窗口大小的范围内取值
  //tell the PID to range between 0 and the full window size
  myPID.SetOutputLimits(0, WindowSize);
  //开启PID
  //turn the PID on
  myPID.SetMode(AUTOMATIC);
}

void loop()
{
  Input = analogRead(0);
  myPID.Compute();

  /************************************************
   * turn the output pin on/off based on pid output 基于PID输出,打开或关闭端口输出
   ************************************************/
  if(millis() - windowStartTime>WindowSize)
  { //time to shift the Relay Window 继电器窗口时间
    windowStartTime += WindowSize;
  }
  if(Output < millis() - windowStartTime) digitalWrite(RelayPin,HIGH);
  else digitalWrite(RelayPin,LOW);

}

新人感到不解的是。此代码。前面已经定义了windowsStartTime=millis了。后面怎么还能用windowsStartTimes-millis呢?(贴中红字和绿字部分)
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-28 20:56 , Processed in 0.040762 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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