极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10454|回复: 8

超音波传感器得到信息 , 请问要如何输出成 4位数值

[复制链接]
发表于 2013-7-25 16:42:58 | 显示全部楼层 |阅读模式
我从超音波传感器得到信息
她的数值从 个位数 , 十位数 , 百位数都有
如果我想要输出为 4 位数 , 请问要如何处理

例如
        5 ---> 0005
       15---> 0015
      150---> 0150
回复

使用道具 举报

发表于 2013-7-25 17:10:50 | 显示全部楼层
自己写一个转换成四位数的函数。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-7-25 17:28:22 | 显示全部楼层
请问哪里有范例可参考
回复 支持 反对

使用道具 举报

发表于 2013-7-25 18:10:36 | 显示全部楼层
void Convert(int num, char str[5])
{
        unsigned int numIndex = 0;
        unsigned int numCount = 0;
        int tempNum = 0;

        if (num > 10000 || num <= 0)
        {
                return;
        }

        tempNum = num;
        while (tempNum)
        {
                numCount++;
                tempNum /= 10;
        }

        for (numIndex = 0; numIndex < numCount; numIndex++)
        {
                str[numCount - numIndex - 1] = num%10 + '0';
                num /= 10;
        }
}
回复 支持 反对

使用道具 举报

发表于 2013-7-25 21:03:23 | 显示全部楼层
sprintf(str,"%04d",data)
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-7-25 22:30:52 | 显示全部楼层
下面程序 compiler 有误 , 请问如何修改
sketch_jul25a.ino: In function 'int Convert(int, char*)':
sketch_jul25a:26: error: return-statement with no value, in function returning 'int'

int num;
char str[5];
void setup()
{
}

void loop()
{
  num = 999;
  Convert(num,str);
  Serial.print(num);
}





int Convert(int num, char str[5])
{
         unsigned int numIndex = 0;
         unsigned int numCount = 0;
         int tempNum = 0;

         if (num > 10000 || num <= 0)
         {
                 return;
         }

         tempNum = num;
         while (tempNum)
         {
                 numCount++;
                 tempNum /= 10;
         }

         for (numIndex = 0; numIndex < numCount; numIndex++)
         {
                 str[numCount - numIndex - 1] = num%10 + '0';
                 num /= 10;
         }
         return num;
}
回复 支持 反对

使用道具 举报

发表于 2013-7-26 09:35:25 | 显示全部楼层
楼主还是自己多研究一下c语音吧
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-7-26 14:30:51 | 显示全部楼层
奇点
     谢谢你的帮忙,终于完成我的程序
读取超音波信息,范围从 4cm 到 300cm
我稍微修改一下你教我的程序代码
使得输出统整为4位数
int num;
const int pingPin = 11;
unsigned int duration, cm;

char str[5]="0000";
void setup()
{
   Serial.begin(9600);
}

void loop()
{
   pinMode(pingPin, OUTPUT);          // Set pin to OUTPUT
  digitalWrite(pingPin, LOW);        // Ensure pin is low
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);       // Start ranging
  delayMicroseconds(5);              //   with 5 microsecond burst
  digitalWrite(pingPin, LOW);        // End ranging
  pinMode(pingPin, INPUT);           // Set pin to INPUT
  duration = pulseIn(pingPin, HIGH); // Read echo pulse
  cm = duration / 74 / 2*2.54;        // Convert to inches
  Convert(cm);
  delay(200);                             // Short delay
}


void Convert(int num)
{
   int numIndex = 0;
   int numCount = 3;
            
        
for (numIndex = 0; numIndex < numCount; numIndex++)
  {
   str[numCount - numIndex] = num%10 + '0';
   num /= 10;
  }
for (numIndex = 0; numIndex <5; numIndex++)
{
  Serial.print(str[numIndex]);
  }
}         
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-7-26 14:33:07 | 显示全部楼层
str[numCount - numIndex] = num%10 + '0';

请问一下 , 为何要加上 '0'        
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-8 16:19 , Processed in 0.039026 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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