Arduinoプログラム集
内容 1. Solenoid_demo_20180924
2. タクトスイッチ5個でソレノイドを動かすための入力値確認用プログラム
3. 超音波スイッチ入力部確認用プログラム
4. 曲げセンサとタクトスイッチの組み合わせ入力の検証
5. タクトスイッチ7個(うち5個のみ使用)でソレノイドを動かすプログラム
6. 光センサで入力し、ソレノイドを動かすプログラム(最終デモ用に使用したもの)
**********************************************************************
1. Solenoid_demo_20180924
********************************************************************** int swVal = 0;
void setup()
{
pinMode(2, OUTPUT);
pinMode(4, INPUT);
}
void loop()
{
swVal=digitalRead(4);
if(swVal == HIGH){
digitalWrite(2,LOW);
} else {
digitalWrite(2,HIGH);
}
}
**********************************************************************
2. タクトスイッチ5個でソレノイドを動かすための入力値確認用プログラム
**********************************************************************
void setup() {
Serial.begin(9600);
int i=0;
int j=0;
}
void loop() {
int i=analogRead(0);
int j = map(i, 0, 1023, 0, 255);
Serial.print(" ");
Serial.print(i);
Serial.print(" ");
delay(1000);
Serial.print(j);
Serial.print(" ");
delay(1000);
}
**********************************************************************
3. 超音波スイッチ入力部確認用プログラム
**********************************************************************
*************************************************
入力値の確認用サンプルプログラム
*************************************************
#include <NewPing.h>
const int PIN_SONAR_TRIGGER = 12;
const int PIN_SONAR_ECHO = 11;
const int MAX_DISTANCE = 200;
NewPing sonar(PIN_SONAR_TRIGGER, PIN_SONAR_ECHO, MAX_DISTANCE);
void setup() {
Serial.begin(9600);
}
void loop() {
delay(50);
unsigned long microSec = sonar.ping_median();
unsigned long dist = sonar.convert_cm(microSec);
if( !dist ){
Serial.print(" ");
Serial.print("-");
return;
}
else{
Serial.print(" ");
Serial.print(dist);
Serial.print("[cm]");
return;
}
}
*************************************************
ソレノイド駆動までを含む確認用サンプルプログラム
*************************************************
#include <SoftwareSerial.h>
#include <NewPing.h>
const int PIN_SONAR_TRIGGER = 12;
const int PIN_SONAR_ECHO = 11;
const int MAX_DISTANCE = 40;
NewPing sonar(PIN_SONAR_TRIGGER, PIN_SONAR_ECHO, MAX_DISTANCE);
int val;
int pos;
int toggle;
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
Serial.begin(9600);
Serial.println("start!");
}
void loop() {
delay(50);
unsigned long microSec = sonar.ping_median();
unsigned long dist = sonar.convert_cm(microSec);
pos = map(dist, 0, MAX_DISTANCE, 0, 6);
toggle = digitalRead(9);
Serial.print(pos);
Serial.print(toggle);
delay(50);
if(pos == 1 and toggle == 1){
digitalWrite(2, LOW);
delay(50);
digitalWrite(2, HIGH);
delay(50);
digitalWrite(2, LOW);
delay(50);
}
else if(pos == 2 and toggle == 1){
digitalWrite(3, LOW);
delay(50);
digitalWrite(3, HIGH);
delay(50);
digitalWrite(3, LOW);
delay(50);
}
else if(pos == 3 and toggle == 1){
digitalWrite(4, LOW);
delay(50);
digitalWrite(4, HIGH);
delay(50);
digitalWrite(4, LOW);
delay(50);
}
else if(pos == 4 and toggle == 1){
digitalWrite(5, LOW);
delay(50);
digitalWrite(5, HIGH);
delay(50);
digitalWrite(5, LOW);
delay(50);
}
else if(pos == 5 and toggle == 1){
digitalWrite(6, LOW);
delay(50);
digitalWrite(6, HIGH);
delay(50);
digitalWrite(6, LOW);
delay(50);
}
}
**********************************************************************
4. 曲げセンサとタクトスイッチの組み合わせ入力の検証
**********************************************************************
*************************************************
入力値の確認用サンプルプログラム
*************************************************
#define ANALOG_PIN 0
#define CENSOR_VALUE_MIN 700
#define CENSOR_VALUE_MAX 800
int val;
int pos;
int tact;
void setup() {
Serial.begin(9600);
pinMode(9, OUTPUT);
}
void loop() {
delay(50);
val = analogRead(ANALOG_PIN);
Serial.println(val);
pos = constrain(val, CENSOR_VALUE_MIN, CENSOR_VALUE_MAX);
pos = map(pos, CENSOR_VALUE_MIN, CENSOR_VALUE_MAX, 0, 5);
Serial.print("曲げ度");
Serial.print(pos);
tact = digitalRead(9);
Serial.print(tact);
return;
delay(500);
}
*************************************************
ソレノイド駆動までを含む確認用サンプルプログラム
*************************************************
#include <SoftwareSerial.h>
#define ANALOG_PIN 0
#define CENSOR_VALUE_MIN 700
#define CENSOR_VALUE_MAX 800
int val;
int pos;
int toggle;
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
Serial.begin(9600);
Serial.println("start!");
}
void loop() {
val = analogRead(ANALOG_PIN);
pos = constrain(val, CENSOR_VALUE_MIN, CENSOR_VALUE_MAX);
pos = map(pos, CENSOR_VALUE_MIN, CENSOR_VALUE_MAX, 0, 5);
toggle = digitalRead(9);
Serial.print(pos);
Serial.print(toggle);
delay(50);
if(pos == 1 and toggle == 1){
digitalWrite(2, LOW);
delay(50);
digitalWrite(2, HIGH);
delay(50);
digitalWrite(2, LOW);
delay(50);
}
else if(pos == 2 and toggle == 1){
digitalWrite(3, LOW);
delay(50);
digitalWrite(3, HIGH);
delay(50);
digitalWrite(3, LOW);
delay(50);
}
else if(pos == 3 and toggle == 1){
digitalWrite(4, LOW);
delay(50);
digitalWrite(4, HIGH);
delay(50);
digitalWrite(4, LOW);
delay(50);
}
else if(pos == 4 and toggle == 1){
digitalWrite(5, LOW);
delay(50);
digitalWrite(5, HIGH);
delay(50);
digitalWrite(5, LOW);
delay(50);
}
else if(pos == 5 and toggle == 1){
digitalWrite(6, LOW);
delay(50);
digitalWrite(6, HIGH);
delay(50);
digitalWrite(6, LOW);
delay(50);
}
}
**********************************************************************
5. タクトスイッチ7個(うち5個のみ使用)でソレノイドを動かすプログラム
**********************************************************************
#include <SoftwareSerial.h>
int s = 0;
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
Serial.begin(9600);
}
void loop() {
s = analogRead(0);
delay(50);
if(1023 < s){
}
else if(1021 < s){ //7 NA
Serial.println("7");
}
else if(851 < s){ //6
Serial.println("6");
digitalWrite(6, LOW);
delay(50);
digitalWrite(6, HIGH);
delay(50);
digitalWrite(6, LOW);
delay(50);}
else if(635 < s){ //5
Serial.println("5");
digitalWrite(5, LOW);
delay(50);
digitalWrite(5, HIGH);
delay(50);
digitalWrite(5, LOW);
delay(50);}
else if(483 < s){ //4
Serial.println("4");
digitalWrite(4, LOW);
delay(50);
digitalWrite(4, HIGH);
delay(50);
digitalWrite(4, LOW);
delay(50);}
else if(340 < s){ //3
Serial.println("3");
digitalWrite(3, LOW);
delay(50);
digitalWrite(3, HIGH);
delay(50);
digitalWrite(3, LOW);
delay(50);}
else if(221 < s){ //2
Serial.println("2");
digitalWrite(2, LOW);
delay(50);
digitalWrite(2, HIGH);
delay(50);
digitalWrite(2, LOW);
delay(50);}
else if(0 < s){ //1 NA
Serial.println("1");
}
delay(100);
Serial.println(s);
}
**********************************************************************
6. 光センサで入力し、ソレノイドを動かすプログラム(最終デモ用に使用したもの)
**********************************************************************
#include <Wire.h>
#include <VL53L0X.h>
#include <SoftwareSerial.h>
VL53L0X sensor;
const int PIN_SONAR_TRIGGER = 12;
const int PIN_SONAR_ECHO = 11;
const int MAX_DISTANCE = 300;
int val;
int pos;
int toggle;
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, INPUT);
Serial.begin(9600);
Wire.begin();
Serial.println("start!");
sensor.init();
sensor.setTimeout(500);
sensor.startContinuous();
}
void loop() {
Serial.print(sensor.readRangeContinuousMillimeters());
if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
Serial.println();
delay(50);
unsigned long dist = (sensor.readRangeContinuousMillimeters());
pos = map(dist, 0, MAX_DISTANCE, 0, 6);
toggle = digitalRead(9);
Serial.print(pos);
Serial.print(toggle);
delay(50);
if(pos == 1 and toggle == 1){
digitalWrite(4, LOW);
delay(50);
digitalWrite(4, HIGH);
delay(50);
digitalWrite(4, LOW);
delay(50);
}
else if(pos == 2 and toggle == 1){
digitalWrite(3, LOW);
delay(50);
digitalWrite(3, HIGH);
delay(50);
digitalWrite(3, LOW);
delay(50);
}
else if(pos == 3 and toggle == 1){
digitalWrite(6, LOW);
delay(50);
digitalWrite(6, HIGH);
delay(50);
digitalWrite(6, LOW);
delay(50);
}
else if(pos == 4 and toggle == 1){
digitalWrite(2, LOW);
delay(50);
digitalWrite(2, HIGH);
delay(50);
digitalWrite(2, LOW);
delay(50);
}
else if(pos == 5 and toggle == 1){
digitalWrite(5, LOW);
delay(50);
digitalWrite(5, HIGH);
delay(50);
digitalWrite(5, LOW);
delay(50);
}
}