1
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Arduino

Last updated at Posted at 2021-07-12

Arduinoの接続

使用するピンに名前を付ける

以下のように名前を付けておくと、プログラミングの際に便利です。

#define LED_PIN1 13
#define LED_PIN2 12
#define LED_PIN3 11
#define LED_PIN4 10

10回点滅させるための関数

void blink10(){
  int i;
  for(i=0;i<10;i++){
    digitalWrite(LED_PIN1, HIGH);
    digitalWrite(LED_PIN2, LOW);
    digitalWrite(LED_PIN3, LOW);
    digitalWrite(LED_PIN4, LOW);

    delay(200);
    digitalWrite(LED_PIN1, LOW);
    delay(200);
  }
}

アウトプットなどを指定

void setup() {
  pinMode(LED_PIN1, OUTPUT);
  pinMode(LED_PIN2, OUTPUT);
  pinMode(LED_PIN3, OUTPUT);
  pinMode(LED_PIN4, OUTPUT);
  Serial.begin(9600);
  digitalWrite(LED_PIN2, HIGH);
  }

ループ関数

シリアルで'a'を入力した場合、緑を点灯
シリアルで'b'を入力した場合、黄色を点灯
シリアルで'c'を入力した場合、赤を10回点滅させてから点灯

void loop() {
  int inputchar;
  
  inputchar = Serial.read();
  if (inputchar != -1){
    
    switch(inputchar){
      case 'a':
       Serial.print("GR ON\n");
       digitalWrite(LED_PIN1, LOW);
       digitalWrite(LED_PIN2, LOW);
       digitalWrite(LED_PIN3, HIGH);
       digitalWrite(LED_PIN4, LOW);
       break;
      case 'b':
       Serial.print("YE ON\n");
       digitalWrite(LED_PIN1, LOW);
       digitalWrite(LED_PIN2, LOW);
       digitalWrite(LED_PIN3, LOW);
       digitalWrite(LED_PIN4, HIGH);
       
       break;
      case 'c':
       Serial.print("RED ON\n");
       blink10();
       digitalWrite(LED_PIN1, HIGH);
       break;
    }
    Serial.print("READY\n");
  }
}

長押しを認識させてシリアル出力

#define PUSH_SHORT 100
#define PUSH_LONG 32000
uint16_t count_low = 0;

void setup ( ) {
  Serial.begin(9600);               // 9600bpsでシリアルポートを開く
  pinMode(2,INPUT_PULLUP);
  }
  
void loop ( ) { 
  if(digitalRead(2) == LOW){        //スイッチが押されているとき
    if(count_low <= PUSH_LONG)count_low ++;   }
    else{                            //スイッチが押されていないとき 
      if(count_low>= PUSH_LONG)Serial.println("long");
    else if(count_low >= PUSH_SHORT)Serial.println("short");
    
    count_low = 0;
  }
}

カンマ区切りでシリアル送信

int i = 0;
char buf[20];
int ch[5];

void setup() {
  Serial.begin(9600);
}

void loop() {
 if (Serial.available()) {
    buf[i] = Serial.read();
    if (buf[i] == 'e') {
      buf[i] = '\0';
      //Serial.println(buf);

      ch[0] = atoi(strtok(buf, ","));
      ch[1] = atoi(strtok(NULL, ","));
      ch[2] = atoi(strtok(NULL, ","));

      Serial.println(ch[0]);
      Serial.println(ch[1]);
      Serial.println(ch[2]);
      i = 0;
    }
    else {
      i++;
    }
  }
}

#音を鳴らす

const unsigned char sample_raw[] PROGMEM = {
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x7f,0x80,0x80,0x80,0x7f,0x80,0x80,
0x6d,0x80,0x5b,0x7f,0x5e,0x80,0x6c,0x7f,0x59,0x80,0x6f,0x7f,0x85,0x7f,0x9a,0x80
};
unsigned int sample_raw_len = 1200;

void setup() 
{
  pinMode(3, OUTPUT);
  TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
  TCCR2B = _BV(CS20);
  
  play();
}

void play() {
  for (int i = 0; i < sample_raw_len; i++) {
    OCR2B = pgm_read_byte_near(&sample_raw[i]);
    delayMicroseconds(125);
  }
} 

void loop() {
}
1
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?