2017検索大隊電子信号機アドゥエノプロジェクト


Master part code

#include <SoftwareSerial.h>
#include <Adafruit_NeoPixel.h>

SoftwareSerial mySerial(2, 3); // RX, TX
int BUTTON1 = 4;
int BUTTON2 = 5;
int BUTTON3 = 6;
int pot = A0;
int btnstate1;
int prevstate1 = 1;
int btnstate2;
int prevstate2 = 1;
int btnstate3;
int prevstate3 = 1;
int num1 = 0;
int num2 = 0;
int num3 = 0;
int lednum1 = 0;
int lednum2 = 0;
int lednum3 = 0;
int lednum4 = 0;
int lednum5 = 0;
int lednum6 = 0;
#define PIN 7
#define LEDNUM 3
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDNUM, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  Serial.begin(9600);
  mySerial.begin(4800);
  int reading = analogRead(pot);
  int b = map(reading,0,1024,1,150);
    strip.begin();    
  strip.setPixelColor(0, b, 0, 0);
  strip.show();     
  delay(400);
  strip.setPixelColor(1, b, b, 0);
  strip.show();     
  delay(400);
  strip.setPixelColor(2, 0, b, 0);
  strip.show();     
  delay(400);
  int k = 0;
  while(k < 4)
  {
    strip.setPixelColor(0, b, 0, 0);
    strip.setPixelColor(1, b, b, 0);
    strip.setPixelColor(2, 0, b, 0);
    strip.show();
    delay(200);
  strip.setPixelColor(0, 0, 0, 0);
  strip.setPixelColor(1, 0, 0, 0);
  strip.setPixelColor(2, 0, 0, 0);
  strip.show();
  delay(200);
  k++;
  }
}

void loop() {
  int reading = analogRead(pot);
int b = map(reading,0,1024,1,150);

  if (Serial.available()) {
    char c = (char)Serial.read();
   if (c == 'm'){
         mySerial.print("m");
  strip.setPixelColor(2, b, 0, 0);
    strip.show();
  }
   if (c == 'n'){
         mySerial.print("n");
  strip.setPixelColor(2, 0, 0, 0);
    strip.show();
  }
   if (c == 'o'){
         mySerial.print("o");
  strip.setPixelColor(2, b, b, 0);
    strip.show();
  }
   if (c == 'p'){
         mySerial.print("p");
  strip.setPixelColor(2, 0, 0, 0);
    strip.show();
  }
   if (c == 'q'){
         mySerial.print("q");
  strip.setPixelColor(2, 0, b, 0);
    strip.show();
  }
   if (c == 'r'){
         mySerial.print("r");
  strip.setPixelColor(2, 0, 0, 0);
    strip.show();
  }
  }

    if (mySerial.available()) {
    char c = (char)mySerial.read();
   if (c == 'g'){
        Serial.print("g");
  strip.setPixelColor(0, b, 0, 0);
    strip.show();
  }
   if (c == 'h'){
        Serial.print("h");
  strip.setPixelColor(0, 0, 0, 0);
    strip.show();
  }
   if (c == 'i'){
        Serial.print("i");
  strip.setPixelColor(0, b, b, 0);
    strip.show();
  }
   if (c == 'j'){
        Serial.print("j");
  strip.setPixelColor(0, 0, 0, 0);
    strip.show();
  }
   if (c == 'k'){
        Serial.print("k");
  strip.setPixelColor(0, 0, b, 0);
    strip.show();
  }
   if (c == 'l'){
        Serial.print("l");
  strip.setPixelColor(0, 0, 0, 0);
    strip.show();
  }
  }

  
    btnstate1 = digitalRead(BUTTON1);
if(btnstate1 != prevstate1){
  if(btnstate1 ==1){
    if(num1 == 0){
      mySerial.print("a");
    Serial.print("a");
  strip.setPixelColor(1, b, 0, 0);
    strip.show();
        num1 ++;
  }
  else {
    mySerial.print("b");    
    Serial.print("b");
  strip.setPixelColor(1, 0, 0, 0);
    strip.show();
        num1 = 0;
  }
  }
  prevstate1 = btnstate1;
}

    btnstate2 = digitalRead(BUTTON2);
if(btnstate2 != prevstate2){
  if(btnstate2 ==1){
    if(num2 == 0){
      mySerial.print("c");
    Serial.print("c");
  strip.setPixelColor(1, b, b, 0);
    strip.show();
        num2 ++;
  }
  else {    
    mySerial.print("d");
    Serial.print("d");
  strip.setPixelColor(1, 0, 0, 0);
    strip.show();
        num2 = 0;
  }
  }
  prevstate2 = btnstate2;
}

btnstate3 = digitalRead(BUTTON3);
if(btnstate3 != prevstate3){
  if(btnstate3 ==1){
    if(num3 == 0){
      mySerial.print("e");
    Serial.print("e");
  strip.setPixelColor(1, 0, b, 0);
    strip.show();
        num3 ++;
  }
  else {    
    mySerial.print("f");
    Serial.print("f");
  strip.setPixelColor(1, 0, 0, 0);
    strip.show();
        num3 = 0;
  }
  }
  prevstate3 = btnstate3;
}
delay(10);
}

Slave part code (HardwareSerial)

#include <Adafruit_NeoPixel.h>

int BUTTON1 = 4;
int BUTTON2 = 5;
int BUTTON3 = 6;
int pot = A0;
int btnstate1;
int prevstate1 = 1;
int btnstate2;
int prevstate2 = 1;
int btnstate3;
int prevstate3 = 1;
int num1 = 0;
int num2 = 0;
int num3 = 0;
int lednum1 = 0;
int lednum2 = 0;
int lednum3 = 0;
int lednum4 = 0;
int lednum5 = 0;
int lednum6 = 0;

#define PIN 7
#define LEDNUM 3
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDNUM, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  Serial.begin(9600);
  strip.begin();                           // 네오픽셀 제어시작
  strip.show();                            // 네오픽셀 초기화
  int reading = analogRead(pot);
  int b = map(reading,0,1024,1,150);
    strip.begin();    
  strip.setPixelColor(0, b, 0, 0);
  strip.show();     
  delay(400);
  strip.setPixelColor(1, b, b, 0);
  strip.show();     
  delay(400);
  strip.setPixelColor(2, 0, b, 0);
  strip.show();     
  delay(400);
  int k = 0;
  while(k < 4)
  {
    strip.setPixelColor(0, b, 0, 0);
    strip.setPixelColor(1, b, b, 0);
    strip.setPixelColor(2, 0, b, 0);
    strip.show();
    delay(200);
  strip.setPixelColor(0, 0, 0, 0);
  strip.setPixelColor(1, 0, 0, 0);
  strip.setPixelColor(2, 0, 0, 0);
  strip.show();
  delay(200);
  k++;
  }
}

void loop() {
int reading = analogRead(pot);
int b = map(reading,0,1024,1,150);

  if (Serial.available()) {
    char c = (char)Serial.read();
   if (c == 'a'){
  strip.setPixelColor(1, b, 0, 0);
    strip.show();
  }
   if (c == 'b'){
  strip.setPixelColor(1, 0, 0, 0);
    strip.show();
  }
   if (c == 'c'){
  strip.setPixelColor(1, b, b, 0);
    strip.show();
  }
   if (c == 'd'){
  strip.setPixelColor(1, 0, 0, 0);
    strip.show();
  }
   if (c == 'e'){
  strip.setPixelColor(1, 0, b, 0);
    strip.show();
  }
   if (c == 'f'){
  strip.setPixelColor(1, 0, 0, 0);
    strip.show();
  }
   if (c == 'g'){
  strip.setPixelColor(0, b, 0, 0);
    strip.show();
  }
   if (c == 'h'){
  strip.setPixelColor(0, 0, 0, 0);
    strip.show();
  }
   if (c == 'i'){
  strip.setPixelColor(0, b, b, 0);
    strip.show();
  }
   if (c == 'j'){
  strip.setPixelColor(0, 0, 0, 0);
    strip.show();
  }
   if (c == 'k'){
  strip.setPixelColor(0, 0, b, 0);
    strip.show();
  }
   if (c == 'l'){
  strip.setPixelColor(0, 0, 0, 0);
    strip.show();
  }
  }


 
    btnstate1 = digitalRead(BUTTON1);
if(btnstate1 != prevstate1){
  if(btnstate1 ==1){
    if(num1 == 0){
      Serial.print("m");
  strip.setPixelColor(2, b, 0, 0);
    strip.show();
        num1 ++;
  }
  else {
    Serial.print("n");    
  strip.setPixelColor(2, 0, 0, 0);
    strip.show();
        num1 = 0;
  }
  }
  prevstate1 = btnstate1;
}

    btnstate2 = digitalRead(BUTTON2);
if(btnstate2 != prevstate2){
  if(btnstate2 ==1){
    if(num2 == 0){
      Serial.print("o");
  strip.setPixelColor(2, b, b, 0);
    strip.show();
        num2 ++;
  }
  else {    
    Serial.print("p");
  strip.setPixelColor(2, 0, 0, 0);
    strip.show();
        num2 = 0;
  }
  }
  prevstate2 = btnstate2;
}

btnstate3 = digitalRead(BUTTON3);
if(btnstate3 != prevstate3){
  if(btnstate3 ==1){
    if(num3 == 0){
      Serial.print("q");
  strip.setPixelColor(2, 0, b, 0);
    strip.show();
        num3 ++;
  }
  else {    
    Serial.print("r");
  strip.setPixelColor(2, 0, 0, 0);
    strip.show();
        num3 = 0;
  }
  }
  prevstate3 = btnstate3;
}
delay(10);
}

Slave part code (SoftwareSerial)

#include <SoftwareSerial.h>
#include <Adafruit_NeoPixel.h>

SoftwareSerial mySerial(2, 3);
int BUTTON1 = 4;
int BUTTON2 = 5;
int BUTTON3 = 6;
int pot = A0;
int btnstate1;
int prevstate1 = 1;
int btnstate2;
int prevstate2 = 1;
int btnstate3;
int prevstate3 = 1;
int num1 = 0;
int num2 = 0;
int num3 = 0;
int lednum1 = 0;
int lednum2 = 0;
int lednum3 = 0;
int lednum4 = 0;
int lednum5 = 0;
int lednum6 = 0;

#define PIN 7
#define LEDNUM 3
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDNUM, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  mySerial.begin(4800);
      strip.begin();                           // 네오픽셀 제어시작
  strip.show();                            // 네오픽셀 초기화
  int reading = analogRead(pot);
  int b = map(reading,0,1024,1,150);
    strip.begin();    
  strip.setPixelColor(0, b, 0, 0);
  strip.show();     
  delay(400);
  strip.setPixelColor(1, b, b, 0);
  strip.show();     
  delay(400);
  strip.setPixelColor(2, 0, b, 0);
  strip.show();     
  delay(400);
  int k = 0;
  while(k < 4)
  {
    strip.setPixelColor(0, b, 0, 0);
    strip.setPixelColor(1, b, b, 0);
    strip.setPixelColor(2, 0, b, 0);
    strip.show();
    delay(200);
  strip.setPixelColor(0, 0, 0, 0);
  strip.setPixelColor(1, 0, 0, 0);
  strip.setPixelColor(2, 0, 0, 0);
  strip.show();
  delay(200);
  k++;
  }
}

void loop() {
int reading = analogRead(pot);
int b = map(reading,0,1024,1,150);
  
  if (mySerial.available()) {
    char c = (char)mySerial.read();
   if (c == 'a'){
  strip.setPixelColor(1, b, 0, 0);
    strip.show();
  }
   if (c == 'b'){
  strip.setPixelColor(1, 0, 0, 0);
    strip.show();
  }
   if (c == 'c'){
  strip.setPixelColor(1, b, b, 0);
    strip.show();
  }
   if (c == 'd'){
  strip.setPixelColor(1, 0, 0, 0);
    strip.show();
  }
   if (c == 'e'){
  strip.setPixelColor(1, 0, b, 0);
    strip.show();
  }
   if (c == 'f'){
  strip.setPixelColor(1, 0, 0, 0);
    strip.show();
  }
   if (c == 'm'){
  strip.setPixelColor(2, b, 0, 0);
    strip.show();
  }
   if (c == 'n'){
  strip.setPixelColor(2, 0, 0, 0);
    strip.show();
  }
   if (c == 'o'){
  strip.setPixelColor(2, b, b, 0);
    strip.show();
  }
   if (c == 'p'){
  strip.setPixelColor(2, 0, 0, 0);
    strip.show();
  }
   if (c == 'q'){
  strip.setPixelColor(2, 0, b, 0);
    strip.show();
  }
   if (c == 'r'){
  strip.setPixelColor(2, 0, 0, 0);
    strip.show();
  }
  }

    btnstate1 = digitalRead(BUTTON1);
if(btnstate1 != prevstate1){
  if(btnstate1 ==1){
    if(num1 == 0){
      mySerial.print("g");
  strip.setPixelColor(0, b, 0, 0);
    strip.show();
        num1 ++;
  }
  else {
    mySerial.print("h");    
  strip.setPixelColor(0, 0, 0, 0);
    strip.show();
        num1 = 0;
  }
  }
  prevstate1 = btnstate1;
}

    btnstate2 = digitalRead(BUTTON2);
if(btnstate2 != prevstate2){
  if(btnstate2 ==1){
    if(num2 == 0){
      mySerial.print("i");
  strip.setPixelColor(0, b, b, 0);
    strip.show();
        num2 ++;
  }
  else {    
    mySerial.print("j");
  strip.setPixelColor(0, 0, 0, 0);
    strip.show();
        num2 = 0;
  }
  }
  prevstate2 = btnstate2;
}

btnstate3 = digitalRead(BUTTON3);
if(btnstate3 != prevstate3){
  if(btnstate3 ==1){
    if(num3 == 0){
      mySerial.print("k");
  strip.setPixelColor(0, 0, b, 0);
    strip.show();
        num3 ++;
  }
  else {    
    mySerial.print("l");
  strip.setPixelColor(0, 0, 0, 0);
    strip.show();
        num3 = 0;
  }
  }
  prevstate3 = btnstate3;
}
delay(10);
}