Uit Hack42
Ga naar: navigatie, zoeken
k
 
(5 tussenliggende versies door 3 gebruikers niet weergegeven)
Regel 2: Regel 2:
 
|Naam=Butterfly Puzzle
 
|Naam=Butterfly Puzzle
 
|Eigenaar=Moem, Eightdot, SA007, WitchDoc, PeterKKN
 
|Eigenaar=Moem, Eightdot, SA007, WitchDoc, PeterKKN
|Status=Uitvoer
+
|Status=Afgerond
 
|Skills=lassen schilderen elektronica software
 
|Skills=lassen schilderen elektronica software
 +
|Samenvatting=Butterfly puzzle voor Escape Room Rozet
 
}}
 
}}
 
<nowiki>
 
 
code:
 
code:
 
+
<pre>
 
 
 
#include "Adafruit_WS2801.h"
 
#include "Adafruit_WS2801.h"
 
//#include "Color.h"
 
//#include "Color.h"
Regel 29: Regel 27:
 
Color grn = 0x0000ff00;  //Color(0, 255, 0);
 
Color grn = 0x0000ff00;  //Color(0, 255, 0);
  
/* bottom views
+
/* top views
 
  * BOARD LAYOUT
 
  * BOARD LAYOUT
 
  *  ------------------
 
  *  ------------------
 
  *  |                |     
 
  *  |                |     
  *  |  A3   A0   9 |    9=A9
+
  *  |  A9   A0   A3 |    9=A9
 
  *  |                |
 
  *  |                |
  *  |  6   A2    8 |    6=A7 8=A8
+
  *  | A8   A2   A7 |    6=A7 8=A8
 
  *  |                |
 
  *  |                |
  *  |   4   A1  NC |    4=A6
+
  *  | NC   A1  A6 |    4=A6
 
  *  |                |
 
  *  |                |
 
  *  ------------------
 
  *  ------------------
Regel 44: Regel 42:
 
  *  ------------------
 
  *  ------------------
 
  *  |                |     
 
  *  |                |     
  *  |  2    3    3 |    2  2  1
+
  *  |  1    2    2 |     
 
  *  |                |
 
  *  |                |
  *  |  2   4    1 |    3  4  1
+
  *  |  1   4    3 |     
 
  *  |                |
 
  *  |                |
  *  |  1    1   NC 1|   3  1  NC 
+
  *  |  NC   1    3 
 
  *  |                |
 
  *  |                |
 
  *  ------------------
 
  *  ------------------
Regel 59: Regel 57:
 
  *  |  3    4    5  |
 
  *  |  3    4    5  |
 
  *  |                |
 
  *  |                |
  *  |  6    7   NC |
+
  *  |   NC   6    7  |
 
  *  |                |
 
  *  |                |
 
  *  ------------------
 
  *  ------------------
Regel 68: Regel 66:
  
 
//uint8_t order[8] = {2, 3, 3, 2, 4, 1, 1, 1};
 
//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 order[8] = {1, 2, 2, 1, 4, 3, 1, 3};
uint8_t analog_channels[8] = {A3, A0, A9, A7, A2, A8, A6, A1};
+
uint8_t analog_channels[8] = {A9, A0, A3, A8, A2, A7, A1, A6};
  
 
//                white blue ^red  _red  
 
//                white blue ^red  _red  
 
//uint8_t val[4] = {204, 408, 612, 816};
 
//uint8_t val[4] = {204, 408, 612, 816};
#define TYPE1 816       //red down
+
#define TYPE1L 677       //red down
#define TYPE2 408       //blue
+
#define TYPE2L 375       //blue
#define TYPE3 612       //red top (should be 204 white)
+
#define TYPE3L 204      //white
#define TYPE4 204       //white   (should be 612 red top)
+
#define TYPE4L 532       //red top
uint16_t val[4] = {TYPE1, TYPE2, TYPE3, TYPE4};
+
#define TYPE1H 816      //red down
 +
#define TYPE2H 408      //blue
 +
#define TYPE3H 216       //white
 +
#define TYPE4H 612       //red top
 +
#define TYPEX 816      //defective open circuit
 +
uint16_t lval[4] = {TYPE1L, TYPE2L, TYPE3L, TYPE4L};
 +
uint16_t hval[4] = {TYPE1H, TYPE2H, TYPE3H, TYPE4H};
 
uint8_t state;
 
uint8_t state;
 +
 +
void myprints(char * str)
 +
{
 +
  if(Serial)
 +
  {
 +
    Serial.print(str);
 +
  }
 +
}
 +
void myprintc(char str)
 +
{
 +
  if(Serial)
 +
  {
 +
    Serial.print(str);
 +
  }
 +
}
 +
void myprinti(long int str)
 +
{
 +
  if(Serial)
 +
  {
 +
    Serial.print(str);
 +
  }
 +
}
  
 
void setup(void) {
 
void setup(void) {
Regel 104: Regel 130:
  
 
}
 
}
 +
// no more Serial, will hang if not connected!
 +
#define Serial qqqqq 
 +
 +
  
 
void loop() {
 
void loop() {
   uint8_t i, j, error, inserted, correct;
+
   uint8_t i, j, error=0, inserted=0, correct=0;
 
   uint16_t res=0;
 
   uint16_t res=0;
 
   uint8_t ok=0;
 
   uint8_t ok=0;
Regel 115: Regel 145:
 
     if((i % 3) == 0)
 
     if((i % 3) == 0)
 
     {
 
     {
       Serial.println("");
+
       myprints("\nanalog: ");
       Serial.print("analog: ");
+
       if(i==6)
 +
        myprints("       ");
 
     }
 
     }
 
     // read value
 
     // read value
 
     res = analogRead(analog_channels[i]);
 
     res = analogRead(analog_channels[i]);
     Serial.print(' ');
+
     myprintc(' ');
     Serial.print(res);
+
     myprinti(res);
 
     if (res < 128) {
 
     if (res < 128) {
 
       // possible shortcut, count as error
 
       // possible shortcut, count as error
 
       error++;
 
       error++;
 
       inserted++;
 
       inserted++;
       Serial.print('*');
+
       myprintc('*');
     }  
+
     } else if(res > TYPEX-MAX_DEVIATION) {
    else if(res < 1024-MAX_DEVIATION)  
+
      if (res < TYPEX + MAX_DEVIATION) {
     {
+
        // possible open circuit, count as error
 +
        error++;
 +
        inserted++;
 +
        myprintc('X');
 +
      }
 +
     } else {
 
       // a butterfly is inserted at this socket
 
       // a butterfly is inserted at this socket
 
       //get type
 
       //get type
Regel 136: Regel 172:
 
       {
 
       {
 
         //check value  //<s>test possible values to determine which type it is</s>
 
         //check value  //<s>test possible values to determine which type it is</s>
         if ((res < (val[j]+MAX_DEVIATION)) && (res > (val[j]-MAX_DEVIATION))) {
+
         if ((res < (hval[j]+MAX_DEVIATION)) && (res > (lval[j]-MAX_DEVIATION))) {
 
           // Count if this is the correct type
 
           // Count if this is the correct type
 
           correct++;
 
           correct++;
           Serial.print('=');
+
           myprintc('=');
         }else if (res < val[j]-MAX_DEVIATION)
+
         }else if (res < lval[j]-MAX_DEVIATION)
 
         {
 
         {
           Serial.print('-');
+
           myprintc('-');
         }else if (res > val[j]+MAX_DEVIATION)
+
         }else if (res > hval[j]+MAX_DEVIATION)
 
         {
 
         {
           Serial.print('+');
+
           myprintc('+');
 
         }
 
         }
 
       }   
 
       }   
Regel 152: Regel 188:
 
     }  
 
     }  
 
   }
 
   }
  Serial.println("");
 
 
   ok=0;    //assume not ok
 
   ok=0;    //assume not ok
 
   if (inserted > 7) {          //was 6
 
   if (inserted > 7) {          //was 6
 
     // All butterflies were inserted
 
     // All butterflies were inserted
     if(error < 2 && correct > 5) {
+
    // TODO error <=2 because testing.
 +
     if((error <= 3) && ((error + correct) > 7)) {
 
       // Minimal amount correct (allow 2 defects), green leds & output high
 
       // Minimal amount correct (allow 2 defects), green leds & output high
 
       ok=2;
 
       ok=2;
Regel 165: Regel 201:
 
     }
 
     }
 
   }
 
   }
   //if(state != ok)
+
   myprints(" ins:");
 +
  myprinti(inserted);
 +
  myprints(" err:");
 +
  myprinti(error);
 +
  myprints(" correct:");
 +
  myprinti(correct);
 +
  myprints(" OK:");
 +
  myprinti(ok);
 +
 
 +
  myprints("\n");
 +
 
 +
  if(state != ok)
 
   {
 
   {
 
     // All butterflies were inserted ok
 
     // All butterflies were inserted ok
Regel 189: Regel 236:
 
     digitalWrite(OUT, j);
 
     digitalWrite(OUT, j);
 
     state = ok;
 
     state = ok;
 +
  } else {
 +
    for(i=0; i<NUMPIXELS; i++) {
 +
      strip1.setPixelColor(i, off);
 +
    }
 +
    strip1.show();
 +
    digitalWrite(OUT, LOW);
 
   }
 
   }
 
   delay(500);
 
   delay(500);
 
}
 
}
</nowiki>
+
</pre>

Huidige versie van 29 nov 2015 om 12:02

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 Afgerond
Madskillz lassen schilderen elektronica software
Doel / Omschrijving
Butterfly puzzle voor Escape Room Rozet
Alle Projecten - Project Toevoegen
File:Butterfly puzzle_Picture.jpg noez

code:

#include "Adafruit_WS2801.h"
//#include "Color.h"
#include "SPI.h" // Comment out this line if using Trinket or Gemma
#define MAX_DEVIATION 80
#define NUMPIXELS 24

#define CLK  2
#define DAT  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);

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


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

//                 white blue ^red   _red 
//uint8_t val[4] = {204, 408, 612, 816};
#define TYPE1L 677       //red down
#define TYPE2L 375       //blue
#define TYPE3L 204       //white
#define TYPE4L 532       //red top
#define TYPE1H 816       //red down
#define TYPE2H 408       //blue
#define TYPE3H 216       //white
#define TYPE4H 612       //red top
#define TYPEX 816       //defective open circuit
uint16_t lval[4] = {TYPE1L, TYPE2L, TYPE3L, TYPE4L};
uint16_t hval[4] = {TYPE1H, TYPE2H, TYPE3H, TYPE4H};
uint8_t state;

void myprints(char * str)
{
  if(Serial)
  {
    Serial.print(str);
  }
}
void myprintc(char str)
{
  if(Serial)
  {
    Serial.print(str);
  }
}
void myprinti(long int str)
{
  if(Serial)
  {
    Serial.print(str);
  }
}

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();

}
// no more Serial, will hang if not connected!
#define Serial qqqqq  



void loop() {
  uint8_t i, j, error=0, inserted=0, correct=0;
  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)
    {
      myprints("\nanalog: ");
      if(i==6)
         myprints("       ");
    }
    // read value
    res = analogRead(analog_channels[i]);
    myprintc(' ');
    myprinti(res);
    if (res < 128) {
      // possible shortcut, count as error
      error++;
      inserted++;
      myprintc('*');
    } else if(res > TYPEX-MAX_DEVIATION) {
      if (res < TYPEX + MAX_DEVIATION) {
        // possible open circuit, count as error
        error++;
        inserted++;
        myprintc('X');
      }
    } else {
      // a butterfly is inserted at this socket
      //get type
      j=order[i]-1;
      //for(j=1; j<5; j++) 
      {
        //check value  //<s>test possible values to determine which type it is</s>
        if ((res < (hval[j]+MAX_DEVIATION)) && (res > (lval[j]-MAX_DEVIATION))) {
          // Count if this is the correct type
          correct++;
          myprintc('=');
        }else if (res < lval[j]-MAX_DEVIATION)
        {
          myprintc('-');
        }else if (res > hval[j]+MAX_DEVIATION)
        {
          myprintc('+');
        }
      }  
      // Count all inserted butterflies
      inserted++;
    } 
  }
  ok=0;    //assume not ok
  if (inserted > 7) {          //was 6
    // All butterflies were inserted
    // TODO error <=2 because testing.
    if((error <= 3) && ((error + correct) > 7)) {
      // Minimal amount correct (allow 2 defects), green leds & output high
      ok=2;
    } 
    else {
      // Not in correct order. red leds and output low
      ok=1;
    }
  }
  myprints(" ins:");
  myprinti(inserted);
  myprints(" err:");
  myprinti(error);
  myprints(" correct:");
  myprinti(correct);
  myprints(" OK:");
  myprinti(ok);
  
  myprints("\n");
  
  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;
  } else {
    for(i=0; i<NUMPIXELS; i++) {
      strip1.setPixelColor(i, off);
    }
    strip1.show();
    digitalWrite(OUT, LOW);
  }
  delay(500);
}