开启左侧

【培训作品】洗碗无忧_章初 林奇峰

[复制链接]
发表于 2017-12-28 23:41:38 | 显示全部楼层 |阅读模式
本帖最后由 george7712 于 2017-12-29 12:05 编辑

洗碗无忧

作品简介:
       该作品主要通过Arduino UNO、i创学院扩展板、LED灯、按钮及蜂鸣器,实现抢答器功能,开始随机生成2二进制数,通过双方各自的两个按钮输入两位二进制数行抢答,
答对者一方LED指示灯闪烁胜出。本设计适用于两口之家,可以有效解决晚餐以后谁来洗碗问题,充分缓解家庭纠纷也可用于KTV代替骰子之功能。
作品照片:
mmexport1514473664712.jpg
源程序代码:
int pin[] = {10, 11, 12, 13};       //每个按键对应的引脚
int preKeyVal[] = {1, 1, 1, 1}; //每个按键先前值
int keyVal[] = {0, 0, 0, 0};   //每个按键的返回值
int keyChanged1 = 0;  //判断1侧按键是否修改,修改的次数
int keyChanged2 = 0;  //判断2侧按键是否修改,修改的次数
int pswVal[] = {2, 3};    //每个按键的对应的密码值

int pswKey1[] = {0, 0};      //1侧最终的密码值
int pswKey2[] = {0, 0};      //2侧最终的密码值
volatile int  p1, p2;
long psw1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
long psw2[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int a = 20;

void setup() {
  // put your setup code here, to run once:
  for (int i = 2; i <= 9; i++) {
    pinMode(i, OUTPUT);
  }
  pinMode(A2, OUTPUT);
  Serial.begin(9600);
  randomSeed(analogRead(0));
  for (int i = 1; i <= 10; i = i + (1)) {
    psw1[(int)(i - 1)] = random(2, 4);
    psw2[(int)(i - 1)] = random(2, 4);
  }
  p1 = psw1[(int)(8)];
  p2 = psw2[(int)(9)];
  Serial.print(p1);
  Serial.println(p2);
  for (int i = 0; i < sizeof(pin) / sizeof(pin[0]); i++) {
    pinMode(pin, INPUT_PULLUP);
  }
  pinMode(10, OUTPUT);
  for (int i = 1; i <= 2; i++) {
    for (int i = 2; i <= 9; i++) {
      digitalWrite(i, HIGH);

      delay(a);
      digitalWrite(i, LOW);
    }
  }
  for (int i = 0; i <= 4; i++) {
    a = a + 30 * i;
    for (int i = 2; i <= 9; i++) {
      digitalWrite(i, HIGH);

      delay(a);
      digitalWrite(i, LOW);

    }
  }
  for (int i = 1; i <= 3; i++) {
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    alarm();
    delay(800);
    digitalWrite(A2, LOW);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    delay(800);
  }
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  delay(500);
  for (int i = 1; i <= 500; i++) {
    digitalWrite(A2, HIGH);
    delay(1.1);
    digitalWrite(A2, LOW);
    delay(1.1);
  }
  digitalWrite(A2, LOW);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  delay(800);


}

void loop() {
  // put your main code here, to run repeatedly:

  for (int i = 0; i < sizeof(pin) / sizeof(pin[0]); i++) {
    keyVal = digitalRead(pin);
    delay(5);
    if (keyVal != preKeyVal) {
      if (keyVal == HIGH) {
        if (i < 2) {
          pswKey1[keyChanged1] = pswVal;
          keyChanged1++;
        }
        else {
          pswKey2[keyChanged2] = pswVal[i - 2];
          keyChanged2++;
        }
      }
    }
  }

  if (keyChanged1 == 2) {  //A侧按键输入两次
    Serial.print("SideA: ");
    Serial.print(pswKey1[0]);
    Serial.print(',');
    Serial.println(pswKey1[1]);
    if (pswKey1[0] == p1 & pswKey1[1] == p2) {
      for (int i = 1; i <= 20; i++) {
        for (int i = 2; i <= 5; i++) {
          digitalWrite(i, HIGH);

          delay(50);
          digitalWrite(i, LOW);
        }alarm2();
      }
    }

    keyChanged1 = 0;
  }
  if (keyChanged2 == 2) {  //B侧按键输入两次
    Serial.print("SideB: ");
    Serial.print(pswKey2[0]);
    Serial.print(',');
    Serial.println(pswKey2[1]);
    if (pswKey2[0] == p1 & pswKey2[1] == p2) {
      for (int i = 1; i <= 20; i++) {
        for (int i = 6; i <= 9; i++) {
          digitalWrite(i, HIGH);
          delay(50);
          digitalWrite(i, LOW);
        }alarm2();
      }
    }
    keyChanged2 = 0;
  }

  for (int i = 0; i < sizeof(pin) / sizeof(pin[0]); i++) {
    preKeyVal = keyVal;
  }

}
void alarm() {
  for (int i = 1; i <= 80; i++) {
    digitalWrite(A2, HIGH);
    delay(3);
    digitalWrite(A2, LOW);
    delay(1);
  }
}
void alarm2() {
  for (int i = 1; i <= 20; i++) {
    digitalWrite(A2, HIGH);
    delay(2);
    digitalWrite(A2, LOW);
    delay(1);
    digitalWrite(A2, HIGH);
    delay(0.5);
    digitalWrite(A2, LOW);
    delay(1);
  }
}
wx_camera_1514140365740.mp4 (1.05 MB, 下载次数: 1)
wx_camera_1514425258720.jpg

点评

很有意思的创意 哈哈  发表于 2017-12-29 16:08
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

精彩课程推荐

关于我们

帮助中心

商务合作

点击这里给我发消息

关注微信公众号

Copyright © 2016-2017 i创学院 - 上海享渔教育科技有限公司 Powered by Discuz!     ( 沪ICP备16033954号-1 )