Uit Hack42
< Gebruiker:Hack42
Versie door Eightdot (overleg | bijdragen) op 24 jul 2015 om 23:54 (Nieuwe pagina aangemaakt met '{{Project |Naam=Butterfly Puzzle |Eigenaar=Moem, Eightdot, SA007, WitchDoc, PeterKKN |Status=Uitvoer |Skills=lassen schilderen elektronica software }} code: #i...')
(wijz) ← Oudere versie | Huidige versie (wijz) | Nieuwere versie → (wijz)
Ga naar: navigatie, zoeken
Project: Butterfly Puzzle
Schroefje24.png
Schroefje24.png
Schroefje24.png
Schroefje24.png
Butterfly puzzle Picture.jpg

Butterfly Puzzle

Naam Butterfly Puzzle
Door Moem, Eightdot, SA007, WitchDoc, PeterKKN
Status Uitvoer
Madskillz lassen schilderen elektronica software
Doel / Omschrijving
{{{Samenvatting}}}
Alle Projecten - Project Toevoegen
File:Butterfly puzzle_Picture.jpg noez

code:


  1. include "Adafruit_WS2801.h"

//#include "Color.h"

  1. include "SPI.h" // Comment out this line if using Trinket or Gemma
  2. define MAX_DEVIATION 80
  3. define NUMPIXELS 24
  1. define CLK 2
  2. define DAT 3
  3. define OUT 16

typedef uint32_t Color;

// Set the first variable to the NUMBER of pixels. 25 = 25 pixels in a row Adafruit_WS2801 strip1 = Adafruit_WS2801(NUMPIXELS, CLK, DAT);

Color off = 0x00000000; // Color(0, 0, 0); Color red = 0x00ff0000; //Color(255, 0, 0); Color grn = 0x0000ff00; //Color(0, 255, 0);

/* bottom views

* BOARD LAYOUT
*  ------------------
*  |                |     
*  |  A3   A0    9  |    9=A9
*  |                |
*  |   6   A2    8  |    6=A7 8=A8
*  |                |
*  |   4   A1   NC  |    4=A6
*  |                |
*  ------------------
*
* TYPE LAYOUT
*  ------------------
*  |                |     
*  |   2    3    3  |    2  2  1
*  |                |
*  |   2    4    1  |    3  4  1
*  |                |
*  |   1    1   NC 1|    3  1  NC  
*  |                |
*  ------------------
*
* READING ORDER
*  ------------------
*  |                |     
*  |   0    1    2  |
*  |                |
*  |   3    4    5  |
*  |                |
*  |   6    7   NC  |
*  |                |
*  ------------------
*
*/


//uint8_t order[8] = {2, 3, 3, 2, 4, 1, 1, 1}; uint8_t order[8] = {2, 2, 1, 3, 4, 1, 3, 1}; uint8_t analog_channels[8] = {A3, A0, A9, A7, A2, A8, A6, A1};

// white blue ^red _red //uint8_t val[4] = {204, 408, 612, 816};

  1. define TYPE1 816 //red down
  2. define TYPE2 408 //blue
  3. define TYPE3 612 //red top (should be 204 white)
  4. define TYPE4 204 //white (should be 612 red top)

uint16_t val[4] = {TYPE1, TYPE2, TYPE3, TYPE4}; uint8_t state;

void setup(void) {

 uint8_t i;
 // Initialize analog channels
 for(i=0; i<8; i++) {
   pinMode(analog_channels[i], INPUT);
 }
 // set pinMode for opto output
 pinMode(OUT, OUTPUT);
 // opto off
 digitalWrite(OUT, LOW);  
 // init state to wrong
 state=0;
 // initialize ledstrip
 strip1.begin();
 strip1.show();
 Serial.begin(9600);
 // All leds off.
 for(i=0; i<NUMPIXELS; i++) {
   strip1.setPixelColor(i, off);
 }
 strip1.show();

}

void loop() {

 uint8_t i, j, error, inserted, correct;
 uint16_t res=0;
 uint8_t ok=0;
 Color col=0;
 inserted =0;
 // for each butterfly (analog input)
 for(i=0; i<8; i++) {
   if((i % 3) == 0)
   {
     Serial.println("");
     Serial.print("analog: ");
   }
   // read value
   res = analogRead(analog_channels[i]);
   Serial.print(' ');
   Serial.print(res);
   if (res < 128) {
     // possible shortcut, count as error
     error++;
     inserted++;
     Serial.print('*');
   } 
   else if(res < 1024-MAX_DEVIATION) 
   {
     // a butterfly is inserted at this socket
     //get type
     j=order[i]-1;
     //for(j=1; j<5; j++) 
     {
       //check value  //test possible values to determine which type it is
       if ((res < (val[j]+MAX_DEVIATION)) && (res > (val[j]-MAX_DEVIATION))) {
         // Count if this is the correct type
         correct++;
         Serial.print('=');
       }else if (res < val[j]-MAX_DEVIATION)
       {
         Serial.print('-');
       }else if (res > val[j]+MAX_DEVIATION)
       {
         Serial.print('+');
       }
     }  
     // Count all inserted butterflies
     inserted++;
   } 
 }
 Serial.println("");
 ok=0;    //assume not ok
 if (inserted > 7) {          //was 6
   // All butterflies were inserted
   if(error < 2 && correct > 5) {
     // Minimal amount correct (allow 2 defects), green leds & output high
     ok=2;
   } 
   else {
     // Not in correct order. red leds and output low
     ok=1;
   }
 }
 //if(state != ok)
 {
   // All butterflies were inserted ok
   if(ok==2) {
     col = grn;
     j=HIGH;
   }else if(ok==1)
   {
     // Not in correct order. red leds and output low
     col = red;
     j=LOW;
   }else{
     // Not enough inserted (yet). leds and output low
     col = off;
     j=LOW;
   }
   // turn on the leds
   for(i=0; i<NUMPIXELS; i++) {
     strip1.setPixelColor(i, col);
   }
   strip1.show();
   // set ooutput
   digitalWrite(OUT, j);
   state = ok;
 }
 delay(500);

}