Skip to content

Commit

Permalink
20241015
Browse files Browse the repository at this point in the history
  • Loading branch information
inafact committed Oct 15, 2024
1 parent a93058e commit 86e347b
Show file tree
Hide file tree
Showing 14 changed files with 592 additions and 0 deletions.
Binary file added 20241015/1015_1-2.pdf
Binary file not shown.
57 changes: 57 additions & 0 deletions 20241015/1015_1_pwm/1015_1_pwm.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* 1015_1_pwm.ino */

// Raspberry Pi Pico W: GPIO16番ピンを _SW1 という名前に、GPIO15番ピンを _LED1 という名前に
// Arduino UNO: 7番ピンを _SW1 という名前に、9番ピンを _LED1 という名前に
#if (defined(PICO_RP2040))
#define _SW1 16
#define _LED1 15
#else
#define _SW1 7
#define _LED1 9
#endif

// デジタル入力読み取り用の変数を設定
int swState = 0;

// PWMのデューティー比(= LEDの明るさ)を記憶する変数を設定
int brightness = 0;

// 経過時間計測用の変数を設定
unsigned long lastPushed;

void setup() {
pinMode(_SW1, INPUT_PULLUP);
pinMode(_LED1, OUTPUT);

lastPushed = millis();

Serial.begin(19200);
}

void loop() {
int tmp_s = !digitalRead(_SW1);

// デジタル入力に変化があった時だけ更新
if (swState != tmp_s) {
swState = tmp_s;
}

// タクトスイッチが押されてる経過時間が200ミリ秒以上の時だけ実行する
if (swState && (millis() - lastPushed) >= 200) {

// デューティー比の値をインクリメント(5ずつ足して、255で余剰演算)
// 0 ~ 254 の間で変化する
brightness = (brightness + 5) % 255;

Serial.print("_SW1_State:");
Serial.print(swState);
Serial.print(",brightness:");
Serial.println(brightness);

// タクトスイッチ押下の経過時間をアップデート
lastPushed = millis();
}

// PWM
analogWrite(_LED1, brightness);
}
74 changes: 74 additions & 0 deletions 20241015/1015_2_pwm_fade/1015_2_pwm_fade.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* 1015_2_pwm_fade.ino */

// Raspberry Pi Pico W: GPIO16番ピンを _SW1 という名前に、GPIO15番ピンを _LED1 という名前に
// Arduino UNO: 7番ピンを _SW1 という名前に、9番ピンを _LED1 という名前に
#if (defined(PICO_RP2040))
#define _SW1 16
#define _LED1 15
#else
#define _SW1 7
#define _LED1 9
#endif

// デジタル入力読み取り用の変数を設定
int swState = 0;

// 角度をを記憶する変数を設定
float deg = 0;

// 角速度を記憶する変数を設定
int speed = 1;

// 経過時間計測用の変数を設定
unsigned long elapsed;

// 経過時間計測(タクトスイッチ押下)用の変数を設定
unsigned long lastPushed;

void setup() {
pinMode(_SW1, INPUT_PULLUP);
pinMode(_LED1, OUTPUT);

lastPushed = millis();
elapsed = micros();

Serial.begin(19200);
}

void loop() {
int tmp_s = !digitalRead(_SW1);

// デジタル入力に変化があった時だけ更新
if (swState != tmp_s) {
swState = tmp_s;
}

// タクトスイッチが押されてる経過時間が200ミリ秒以上の時だけ実行する
if (swState && (millis() - lastPushed) >= 200) {

// ビットシフトを利用した角速度の増加
// 1, 3, 7, ...512, 1023 の間で変化する
speed = (speed << 1) | speed;
if (speed > 1024) { speed = 1; }

Serial.print("_SW1_State:");
Serial.print(swState);
Serial.print(",speed:");
Serial.println(speed);

// タクトスイッチ押下の経過時間をアップデート
lastPushed = millis();
}

// 500マイクロ秒に一回実行
if ((micros() - elapsed) >= 500) {
// cos() 関数を使った周期的なフェードイン・フェードアウト
deg += (speed / 1023.0);
int arc = abs((255 * cos(deg * M_PI / 180.0)));

// PWM
analogWrite(_LED1, arc);

elapsed = micros();
}
}
Binary file added 20241015/1015_3.pdf
Binary file not shown.
59 changes: 59 additions & 0 deletions 20241015/1015_3_pwm_aio/1015_3_pwm_aio.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* 1015_3_pwm_aio.ino */

// Raspberry Pi Pico W: GPIO16番ピンを _SW1 という名前に、GPIO15番ピンを _LED1 という名前に
// Arduino UNO: 7番ピンを _SW1 という名前に、9番ピンを _LED1 という名前に
#if (defined(PICO_RP2040))
#define _CDS1 A0
#define _LED1 15
#else
#define _CDS1 A0
#define _LED1 9
#endif

// アナログ入力読み取り用のk各種変数(CdSセルの状態を記憶する領域)を設定する
// ノイズ対策のために、10サンプルの平均値を取る方法を採用
const int bufSize = 10;
int ainBuf[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int bIndex = 0;
int ainTotal = 0;

// 経過時間計測用の変数を設定
unsigned long elapsed;

void setup() {
pinMode(_CDS1, INPUT);
pinMode(_LED1, OUTPUT);

elapsed = millis();

Serial.begin(19200);
}

void loop() {
// 直近10サンプルの平均値を取る
ainTotal = ainTotal - ainBuf[bIndex];

ainBuf[bIndex] = analogRead(_CDS1);

ainTotal = ainTotal + ainBuf[bIndex];

bIndex = (bIndex + 1) % bufSize;

int avg = ainTotal / bufSize;

// PWM
// map() 関数を使って、マッピングする
// 周囲の環境が明るいほど明るく点灯
analogWrite(_LED1, map(avg, 5, 900, 0, 254));

// 反転版。周囲の環境が暗いほど明るく点灯
// analogWrite(_LED1, map(avg, 5, 900, 254, 0));

// 50ミリ秒に一回実行
if ((millis() - elapsed) >= 50) {
Serial.println(avg);
elapsed = millis();
}

delay(1);
}
33 changes: 33 additions & 0 deletions 20241015/1015_4_serial_ide/1015_4_serial_ide.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* 1015_4_serial_ide.ino */

// Raspberry Pi Pico W: GPIO16番ピンを _SW1 という名前に、GPIO15番ピンを _LED1 という名前に
// Arduino UNO: 7番ピンを _SW1 という名前に、9番ピンを _LED1 という名前に
#if (defined(PICO_RP2040))
#define _LED1 15
#else
#define _LED1 9
#endif

int swState = 0;
int brightness = 0;

void setup() {
pinMode(_LED1, OUTPUT);

Serial.begin(115200);
}

void loop() {
// シリアルポートへデータが送られてきているかをチェック
if (Serial.available()) {
// シリアルバッファにあるデータを String(文字列)として取り出す
String s = Serial.readString();

// 文字列を int(数値)として変換
brightness = constrain(s.toInt(), 0, 254);

Serial.println(brightness);
}

analogWrite(_LED1, brightness);
}
Binary file added 20241015/1015_5.pdf
Binary file not shown.
110 changes: 110 additions & 0 deletions 20241015/1015_n_serial_app/1015_n_serial_app.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/* 1015_n_serial.ino */

// Raspberry Pi Pico W: GPIO16番ピンを _SW1 という名前に、GPIO15番ピンを _LED1 という名前に
// Arduino UNO: 7番ピンを _SW1 という名前に、9番ピンを _LED1 という名前に
#if (defined(PICO_RP2040))
#define _LED1 14
#define _LED2 15
#else
#define _LED1 9
#define _LED2 10
#endif

unsigned long tick = 0;
int brightness = 0;

const byte numChars = 64;
char receivedChars[numChars];

bool newData = false;

void setup() {
pinMode(_LED1, OUTPUT);

Serial.begin(57600);
}

void loop() {
recvWithEndMarker();

if (newData == true) {
String tmp_str = String(receivedChars);
String tmp_perms[2] = { "", "" };
int sidx = 0, pidx = 0;

while (true) {
int fidx = tmp_str.indexOf(' ', sidx);
if (fidx != -1) {
String sstr = tmp_str.substring(sidx, fidx);
sidx = fidx + 1;
if (pidx < 2) {
tmp_perms[pidx] = sstr;
pidx ++;
} else {
break;
}
} else {
String rstr = tmp_str.substring(sidx, tmp_str.length());
if (pidx < 2) {
tmp_perms[pidx] = rstr;
}
break;
}
}

Serial.println(tmp_str);

if (tmp_perms[0].length() != 0) {
int _v0 = tmp_perms[0].toInt();

Serial.print("Raw[0]:");
Serial.print(tmp_perms[0]);
Serial.print(",Int[0]:");
Serial.println(_v0);

analogWrite(_LED1, constrain(_v0, 0, 1023));
}

if (tmp_perms[1].length() != 0) {
int _v1 = tmp_perms[1].toInt();
Serial.print("Raw[1]:");
Serial.print(tmp_perms[1]);
Serial.print(",Int[1]:");
Serial.println(_v1);

analogWrite(_LED2, constrain(_v1, 0, 1023));
}

newData = false;
}

if ((millis() - tick) > 1000) {
tick = millis();
Serial.println("tick");
}
}

void recvWithEndMarker() {
static byte ndx = 0;
const char endMarker = ';';
char rc;

if (Serial.available()) {
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();

if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
}
Loading

0 comments on commit 86e347b

Please sign in to comment.