Cica Mica against ATtiny85

Zápisník experimentátora

Hierarchy: ATtiny85

Little Petrík is no longer Bambíno and the click of the switch is no longer fun. Cica Mica came to his place, and she likes clicks on the switch.Will we repeat the success of the first box in which was the ATtiny13A? We do not know. But now I've used ATtiny85 and a refined program that randomly exchanges 4 LED sequences.

Now I've used a bigger box with a pink top, because little girls have all the things tuned in pink color. I cut the hole in the lid, drilled the four holes, and attached it to the massive switch that is used to control the ceiling lamp. Inside is a microcontroller with three LEDs.

The sketch

The program contains four different sequences that switch with a probability of 6 percent after each step. Sequences are triggered at different speeds, giving the impression that there are more algorithms.

// Sequence 1
const char PROGMEM sequence1[] = {
  0b00000001,
  0b00000010,
  0b00000100,
};
const int sequence_length1 = sizeof(sequence1) / sizeof(char);

// Sequence 2
const char PROGMEM sequence2[] = {
  0b00000001,
  0b00000011,
  0b00000010,
  0b00000110,
  0b00000100,
  0b00000101,
};
const int sequence_length2 = sizeof(sequence2) / sizeof(char);

// Sequence 3
const char PROGMEM sequence3[] = {
  0b00000001,
  0b00000010,
  0b00000100,
  0b00000010,
};
const int sequence_length3 = sizeof(sequence3) / sizeof(char);

// Sequence 4
const char PROGMEM sequence4[] = {
  0b00000001,
  0b00000000,
  0b00000001,
  0b00000000,
  0b00000000,
  0b00000010,
  0b00000000,
  0b00000010,
  0b00000000,
  0b00000000,
  0b00000100,
  0b00000000,
  0b00000100,
  0b00000000,
  0b00000000,
  0b00000010,
  0b00000000,
  0b00000010,
  0b00000000,
  0b00000000,
};
const int sequence_length4 = sizeof(sequence4) / sizeof(char);

class Sequencer {
    char *data;
    int len;
    int pos;

  public:
    Sequencer(char *_data, int _len)
      : data(_data), len(_len), pos(0)
    {}

    void next() {
      char b = pgm_read_byte(&data[pos]);
      digitalWrite(0, b & 0b00000001);
      digitalWrite(1, b & 0b00000010);
      digitalWrite(2, b & 0b00000100);
      pos++;
      if (pos == len)
        pos = 0;
    }
};

Sequencer seq1(sequence1, sequence_length1);
Sequencer seq2(sequence2, sequence_length2);
Sequencer seq3(sequence3, sequence_length3);
Sequencer seq4(sequence4, sequence_length4);
enum states {st1, st2, st3, st4, st5, st6, st7};
states state = st1;

void setup() {
  for (uint8_t i = 0; i < 3; i++)
    pinMode(i, OUTPUT);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
}

void loop() {
  switch (state) {
    case st1:
      seq1.next();
      delay(80);
      break;
    case st2:
      seq2.next();
      delay(80);
      break;
    case st3:
      seq1.next();
      delay(200);
      break;
    case st4:
      seq2.next();
      delay(200);
      break;
    case st5:
      seq3.next();
      delay(200);
      break;
    case st6:
      seq3.next();
      delay(400);
      break;
    case st7:
      seq4.next();
      delay(200);
      break;
  }
  int r = random(100);
  if (r > 94) {
    state = random(7);
  }
}

Source code

The source code is located on the GitHub server.


13.07.2018


Menu