Drucken

SecretBox - Würfel mit Geheimversteck

 

Schaltplan

 

Bauteile

Bauteil  Menge   Einzelpreis  [€]  Summe  [€]
Kunststoffunterlegscheiben 1 3,99 3,99
Metallring mit Feststellschraube 2 0,95 1,90
Acrylfarbe schwarz 1 1,00 1,00
Holz 1 4,37 4,37
Halter für 6 Mignonzellen (AA), Druckknopf 1 0,45 0,45
Kohleschichtwiderstand 1/4W, 5%, 10 K-Ohm 3 0,01 0,03
RAD 100/16 :: Elektrolytkondensator, 6,3x11mm, RM 2,5mm 2 0,04 0,08
MMA8451 Accelerometer Breakout 1 2,99 2,99
Z5U-2,5 100N :: Vielschicht-Keramikkondensator 100N, 20% 1 0,04 0,04
KERKO 22P :: Keramik-Kondensator 22P 2 0,06 0,12
Batterieclip für 9-Volt-Block, vertikal 1 0,22 0,22
Tilt-Switch (Kugelschalter) 1 1,45 1,45
TS 7805 CZ :: U-Reg +5V 1A TO220 1 0,35 0,35
Stiftleisten gerade 1 0,17 0,17
Stiftleisten gewinkelt 1 0,32 0,32
H25SR050 :: Streifenrasterplatine, Hartpapier, 50x100mm 1 0,58 0,58
Standardquarz, Grundton, 16,0 MHz 1 0,16 0,16
AtMega 328 1 4,00 4,00
Kohleschichtwiderstand 1/4W, 5%, 4,7 K-Ohm 1 0,03 0,03
Kohleschichtwiderstand 1/4W, 5%, 1 K-Ohm 1 0,03 0,03
RAD FR 1.000/16 :: Elko radial, 105°C, low ESR, RM 3,5mm 1 0,29 0,29
Batterien LR6 AA 1.5V 6 0,15 0,90
Servo 1 5,95 5,95
      30,37

 

Platine

  

 

Bilder

         

 

Sourcecode

 // Arduino 1.05

// MMA845X // VCC_IN -> 5V // GND -> GND // SCL -> A5 // SDA -> A4

// Motor connection // orange data // red 5V // braun GND

include <Wire.h>
include <Adafruit_MMA8451.h>
include <Adafruit_Sensor.h>
include <Servo.h>
include <MsTimer2.h>
define TIMER2 1000 // Timer event every second (1000ms)
unsigned long idleTime = 0; // Counter for idle time

Adafruit_MMA8451 mma = Adafruit_MMA8451(); // Create tilt sensor object Servo myServo; // Create Servo object

const int powerPin = 2; // Power Pin

int ox, oy, oz; // axis char orientation, orientationPrev; // orientation and previous orientation char pinPrev; // last successful pin value char pin[8] = "1456321"; // pin to open box int pinPos = 0; // current position in pin boolean pass = false; // pin is correct

void setup(void) {

 Serial.begin(9600);
 
 if (! mma.begin()) {
   Serial.println("Couldnt start");
   while (1);
 }
 Serial.println("MMA8451 found!");
 
// 2G -> 4000 // 4G -> 2000 // 8G -> 1000

 mma.setRange(MMA8451_RANGE_2_G);
 
 pinMode(powerPin, OUTPUT);
 digitalWrite(powerPin, HIGH);
 myServo.attach(4);
 MsTimer2::set(TIMER2, checkTimer);
 MsTimer2::start();
}

void loop() {

 // Read the 'raw' data in 14-bit counts
 mma.read();
// Serial.print("X:\t"); Serial.print(mma.x); // Serial.print("\tY:\t"); Serial.print(mma.y); // Serial.print("\tZ:\t"); Serial.print(mma.z); // Serial.println();

 if (mma.x > 3000) // X = 1
   ox = 1;
 else if (mma.x >= -1000 && mma.x <= 1000) // X = 0
   ox = 0;
 else if (mma.x < -3000) // X = -1
   ox = -1;
 if (mma.y > 3000) // Y = 1
   oy = 1;
 else if (mma.y >= -1000 && mma.y <= 1000) // Y = 0
   oy = 0;
 else if (mma.y < -3000) // Y = -1
   oy = -1;
 if (mma.z > 3000) // Z = 1
   oz = 1;
 else if (mma.z >= -1000 && mma.z <= 1000) // Z = 0
   oz = 0;
 else if (mma.z < -3000) // Z = -1
   oz = -1;
 orientationPrev = orientation;
 if (ox == 0 && oy == 0 && oz == 1)
   orientation = '1';
   else if (ox == 0 && oy == 1 && oz == 0)
     orientation = '3';
     else if (ox == 0 && oy == -1 && oz == 0)
       orientation = '4';
       else if (ox == -1 && oy == 0 && oz == 0)
         orientation = '5';
         else if (ox == 1 && oy == 0 && oz == 0)
           orientation = '2';
           else if (ox == 0 && oy == 0 && oz == -1)
             orientation = '6';
 Serial.print("Orientation = ");
 Serial.print(orientation);
 Serial.print(" (prev: ");
 Serial.print(orientationPrev);
 Serial.println(")");
 Serial.println();
 
 if (orientation == orientationPrev)
 {
   Serial.println(pinPos);
   if (pin[pinPos] == orientation)
   {
     pinPos++;
     pinPrev = orientation;
     if (pinPos == 7)
     {
       pass = true;
       pinPos = 0;
       Serial.println("pass OK");
       myServo.write(10);
       delay(300);
       myServo.write(100);
       delay(300);
       delay(1000);
       digitalWrite(powerPin, LOW); // switch off
     }
   }
   else
     pass = false;
     if (pinPrev != orientation)
       pinPos = 0;    // Reset pin
 }
 else 
 {
   idleTime = 0;
 }
 delay(500);
 
}

void checkTimer() {

 idleTime++;
 if (idleTime >= 30) {  // switch off after 30 sec.
   digitalWrite(powerPin, LOW); // switch off
 }
} 

 

Video