carcon999のブログ

12年間Y!ブログの記載を移行しました。電子工作関連の記事が多いです。

arduino開発環境に慣れてみよう

[せっかく動くようになったFUNNELなので、ソフトウェアに慣れてみようとプログラムに励んでみました。

■標準搭載されているLED(P13)を蛍のように点滅させよう。


Arduinoには、Blinkのサンプルプログラムがあるが、点滅だけでは面白くないので、練習を兼ねて蛍のようにジンワリと表現するような表現をさせてみました。まだ、触ったばかりなので、もっと上手い方法があるかもしれませんが、まあ初回ということで。 また、私が使っているのはFunnelですがArduino互換なのでどれでも動くのでしょうね。TIMEの部分の値を変更すると、点滅の速度が変化します。



#define DUTYMAX 32
#define STEP 16
#define TIME 400
int ledPin = 13; // LED connected to digital pin 13

// デューティ比
const short tDuty[DUTYMAX] = {0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0x7FFF, 0x3FFF, 0x1FFF, 0x0FFF, 0x07FF, 0x03FF, 0x01FF, 0x00FF, 0x007F, 0x003F, 0x001F, 0x000F, 0x0007, 0x0003, 0x0001};

void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
}

// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()
{
for(int j = 0 ; j < DUTYMAX ; j++)
{
for(int k = 0 ; k < TIME ; k++)
{
for(int i = 0 ; i < STEP ; i++)
{
if((tDuty[j] >> i ) & 0x0001)
{
digitalWrite(ledPin, HIGH); // set the LED on
}else{
digitalWrite(ledPin, LOW); // set the LED off
}
}
}
}
}