Roboard RB-100 A/D Channel 的應用...

紅外線測距 Sharp GP2Y0A02 成功嚕~

從 I2C 拉出 +5V
然後接上..
A/D Pin 1 (信號)
A/D Pin 2 (接地)
換算電壓 -> 距離, 程式碼如下:

spi_Initialize(SPICLK_21400KHZ );
float volts;
float cm=0;
int channel = 0;
float VOLTS_PER_UNIT = 0.0049F;        // (.0049 for 10 bit A-D)
int val = ad7918_ReadChannel ( channel, //channel = 0 ~ 7
                        AD7918MODE_RANGE_2VREF, AD7918MODE_CODING_1023);
volts = (float)val * VOLTS_PER_UNIT;
cm = 60.495 * pow ( (float)volts, (float)-1.1904);

Part 4 - 控制 servo 轉動的速度, 而且要轉動的 Smooth

執行後是,左右轉到底再反向.


原始碼如下:
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 
void setup() 
{ 
  Serial.begin(9600);
  myservo.attach(9); 
} 
int pulse =2450;
int dir=1;
int offset=1;
void loop() 
{
  myservo.writeMicroseconds( pulse );
  if ( pulse + offset >= 2450 ) dir = -1;
  else if ( pulse - offset <= 600 ) dir = 1;
  
  pulse += ( offset * dir);

  Serial.println ( pulse, DEC );
}