Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include<Servo.h>
#include <SPI.h>
#include<MsTimer2.h>
#include<Time.h>
//7セグを扱う準備
int csPin = 10;
int cycles = 0;
//スピーカーを扱う準備
const int SPEAKER = 2;
//サーボモータを使う準備
int angle[] = {
0, 160};
int value = 0;
const int SERVO = 9;
Servo myservo;
boolean flag = false;
boolean mflag = false;
int m = 0;//分をカウントする為の変数
void setup(){
myservo.attach(SERVO);
pinMode(csPin, OUTPUT);
digitalWrite(csPin, HIGH);
Serial.begin(9600);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV128);
digitalWrite(csPin, LOW);
SPI.transfer('v');
}
void loop(){
spiSendValue(cycles);
//1m経過したら
if((second() + 1) % 60 == 0){
m++;
delay(2000);//重ねてmを加算しないように2秒待つ
}
//調整
//3m14s経過したら
if(value == 0 && ((m + 1) % 4 == 0 && second() == 14)){
mflag = true;
}
//3m経過したら
else if(value == 1 && (m + 1) % 4 == 0){
mflag = true;
}
if((m + 1) % 4 == 0 && mflag){
if(flag){
tone(SPEAKER, 262, 3000);
delay(1000);//1秒停止
tone(SPEAKER, 330, 3000);
delay(1000);
tone(SPEAKER, 392, 3000);
delay(1000);
tone(SPEAKER, 523, 3000);
delay(1000);
myservo.write(angle[value++]);
if(value % 2 == 0){
value = 0;//0,1 で値をループさせる
}
cycles++;//7セグのカウンターをインクリメント
flag = false;
mflag = false;
m = 0;
}
}
else{
flag=true;
}
}
//7セグを使用するための関数
//以下のサイトから引用
//https://github.com/sparkfun/Serial7SegmentDisplay/blob/master/firmware/Serial%207-Segment%20Display/Arduino_Examples/S7S_Example_SPI_Basic/S7S_Example_SPI_Basic.ino
void spiSendValue(int tempCycles)
{
digitalWrite(csPin, LOW);
SPI.transfer(tempCycles / 1000);
tempCycles %= 1000;
SPI.transfer(tempCycles / 100);
tempCycles %= 100;
SPI.transfer(tempCycles / 10);
tempCycles %= 10;
SPI.transfer(tempCycles);
digitalWrite(csPin, HIGH);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.