Code source

  1.  /*
  2.   * Danger Shield Example Sketch
  3.   * Copyright (c) 2010 SparkFun Electronics. All right reserved.
  4.   * Written by Chris Taylor
  5.   *
  6.   * This code was written to demonstrate the Danger Shield from SparkFun Electronics
  7.   *
  8.   * This code will test all of the devices on the Danger Shield one at a time.
  9.   * Press button 1 (D10) to cycle through the different tests. View their output on
  10.   * a terminal running at 9600 baud.
  11.   *
  12.   * http://www.sparkfun.com
  13.   */
  14.  
  15.  // Shift register bit values to display 0-9 on the seven-segment display
  16.  
  17.  const byte ledCharSet[10] = { B00111111,B00000110,B01011011,B01001111,B01100110,B01101101,B01111101,B00000111,B01111111,B01101111};
  18.  
  19.  // Global variables
  20.  int val = 0;
  21.  int state = 0;
  22.  int x = 0;
  23.  int i = 0;
  24.  
  25.  // Pin definitions
  26.  #define SLIDER1 0
  27.  #define SLIDER2 1
  28.  #define SLIDER3 2
  29.  #define KNOCK 5
  30.  #define BUTTON1 10
  31.  #define BUTTON2 11
  32.  #define BUTTON3 12
  33.  #define LED1 5
  34.  #define LED2 6
  35.  #define BUZZER 3
  36.  #define TEMP 4
  37.  #define LIGHT 3
  38.  #define LATCH 7
  39.  #define CLOCK 8
  40.  #define DATA 4
  41.  
  42.  // State machine values
  43.  #define SLIDER_TEST 1
  44.  #define BUZZER_TEST 2
  45.  #define KNOCK_TEST 3
  46.  #define TEMP_TEST 4
  47.  #define LIGHT_TEST 5
  48.  #define BUTTON_TEST 6
  49.  #define SEVENSEG_TEST 7
  50.  
  51.  void setup()
  52.  {
  53.   Serial.begin(9600);
  54.   pinMode(BUZZER, OUTPUT);
  55.   pinMode(LED1, OUTPUT);
  56.   pinMode(LED2, OUTPUT);
  57.   digitalWrite(LED1,HIGH);
  58.   digitalWrite(LED2,HIGH);
  59.   pinMode(LATCH, OUTPUT);
  60.   pinMode(CLOCK, OUTPUT);
  61.   pinMode(DATA,OUTPUT);
  62.  
  63.   Serial.println("Danger Shield Component Test");
  64.   Serial.println("Press Button 1 to begin.");
  65.  }
  66.  
  67.  void loop()
  68.  {
  69.   if(!(digitalRead(BUTTON1))) // Change state
  70.   {
  71.   delay(1); // Debounce
  72.   state++;
  73.  
  74.   if(state > 7){
  75.   state = 1;
  76.   }
  77.   while(!(digitalRead(BUTTON1)));
  78.   }
  79.  
  80.   if(state == SLIDER_TEST) // Displays values of sliders
  81.   {
  82.   Serial.print("Sliders: ");
  83.   val = analogRead(SLIDER1);
  84.   Serial.print(" ");
  85.   Serial.print(val);
  86.   val = analogRead(SLIDER2);
  87.   Serial.print(" ");
  88.   Serial.print(val);
  89.   val = analogRead(SLIDER3);
  90.   Serial.print(" ");
  91.   Serial.println(val);
  92.   delay(300);
  93.   }
  94.  
  95.   if(state == BUZZER_TEST) // Activates buzzer
  96.   {
  97.   for(int x = 0; x < 100; x++)
  98.   {
  99.   digitalWrite(BUZZER, HIGH);
  100.   delay(1);
  101.   digitalWrite(BUZZER, LOW);
  102.   delay(1);
  103.   }
  104.   }
  105.  
  106.   if(state == KNOCK_TEST) // Queries knock sensor
  107.   {
  108.   val = analogRead(KNOCK);
  109.   Serial.print("Knock: ");
  110.   Serial.println(val);
  111.   if(val > 10)
  112.   {
  113.   Serial.println("Knock Received");
  114.   digitalWrite(LED1,HIGH);
  115.   }
  116.   }
  117.  
  118.   if(state == TEMP_TEST) // Displays temp sensor values
  119.   {
  120.   val = analogRead(TEMP);
  121.   Serial.print("Temp: ");
  122.   Serial.println(val);
  123.   }
  124.  
  125.   if(state == LIGHT_TEST) // Displays light sensor values
  126.   {
  127.   val = analogRead(LIGHT);
  128.   Serial.print("Light: ");
  129.   Serial.println(val);
  130.   }
  131.  
  132.   if(state == BUTTON_TEST) // Toggles LED's depending on Buttons 2 and 3
  133.   {
  134.   Serial.println("Button 2 & 3 Test");
  135.   if(digitalRead(BUTTON3))
  136.   {
  137.   digitalWrite(LED1,HIGH);
  138.   }
  139.   else
  140.   {
  141.   digitalWrite(LED1,LOW);
  142.   }
  143.  
  144.   if(digitalRead(BUTTON2))
  145.   {
  146.   digitalWrite(LED2,HIGH);
  147.   }
  148.   else
  149.   {
  150.   digitalWrite(LED2,LOW);
  151.   }
  152.   }
  153.  
  154.   if(state == SEVENSEG_TEST) // Cycles through 0-9 on seven-segment
  155.   {
  156.   i = 0;
  157.   Serial.println("Seven segment display test");
  158.   while(1)
  159.   {
  160.   digitalWrite(LATCH,LOW);
  161.   shiftOut(DATA,CLOCK,MSBFIRST,~(ledCharSet[i]));
  162.   digitalWrite(LATCH,HIGH);
  163.   i++;
  164.   if(i==10){
  165.   i = 0;
  166.   }
  167.   delay(500);
  168.   }
  169.   }
  170.  }