FBSBM


    Hiring Manager interview: Python programmer asked to write
    a bullshit generator, based on N-th order Markov chain.

      http://habrahabr.ru/post/249637/

Cave lighting task
------------------

        s0 s1 s2
         \ | /
      s3 -(x)- s4
         / | \
        s5 s6 s7

There is a cave deep in the center of the mountain. Momentary buttons are near the entance to each corridor to this cave. Build lighting circuit on Atmel AVR ATMega8, Arduino Uno, Raspberry Pi II.

Cave lighting notes
-------------------

Cave-8:

  ..---o---o---o---------..-> GND
  ..o--|-o-|-o-|-o-- R --..-> Pin12
    |  | | | | | |                              |
    R   B   B   B            R -- resistor 1K  -+-
    |                        B -- button     --o o--
  ..o----------..-> +3.3V

Debouncing in hardware:
                                         to Arduino Uno
               +-----------------------> Pin14
               |                -----
  ..+--o---o---o---..-> 3.3V --+S  Q+--> Pin12
  ..o--|-o-|-o-|-o-..-> Pin12   |   |
    |  | | | | | |              |   |
    R   B   B   B             +-R   |
    |                         | -----
  ..o--------------..-> GND   +--------> Pin11

AVR ATMega8
-----------

; ************************************************************
; PROJECT:
; AUTHOR:
; ************************************************************

; Micro + software running
; ------------------------------------------------------------
.MICRO "ATmega8"
.TOOLCHAIN "GCC"
.GCCPATH   "C:\WinAVR"
.GCCMAKE   AUTO
.TARGET    "cave.hex"
.SOURCE    "cave.c"

.TRACE              ; Activate micro trace

; Following lines are optional; if not included
; exactly these values are taken by default
; ------------------------------------------------------------
.POWER VDD=5 VSS=0  ; Power nodes
.CLOCK 1meg         ; Micro clock
.STORE 250m         ; Trace (micro+signals) storage time

; Micro nodes: RESET, AREF, PB0-PB7, PC0-PC6, PD0-PD7, ACO, TIM1OVF, ADC6, ADC7
; Define here the hardware around the micro
; ------------------------------------------------------------

  D1 VDD NS0           ; Is _any_ K0..KF button down?
  K0 NS0 VSS           ; Stimulus: Momentary "SET"
  K1 NS0 VSS           ; Stimulus: Momentary "SET"
  K2 NS0 VSS           ; Stimulus: Momentary "SET"
  K3 NS0 VSS           ; Stimulus: Momentary "SET"
  K4 NS0 VSS           ; Stimulus: Momentary "SET"
  K5 NS0 VSS           ; Stimulus: Momentary "SET"
  K6 NS0 VSS           ; Stimulus: Momentary "SET"
  K7 NS0 VSS           ; Stimulus: Momentary "SET"
  K8 NS0 VSS           ; Stimulus: Momentary "SET"
  K9 NS0 VSS           ; Stimulus: Momentary "SET"
  KA NS0 VSS           ; Stimulus: Momentary "SET"
  KB NS0 VSS           ; Stimulus: Momentary "SET"
  KC NS0 VSS           ; Stimulus: Momentary "SET"
  KD NS0 VSS           ; Stimulus: Momentary "SET"
  KE NS0 VSS           ; Stimulus: Momentary "SET"
  KF NS0 VSS           ; Stimulus: Momentary "SET"
  X0 ND2 NS0 NS0 PB0

  D8 VDD PD0           ; Cave lighting lamps

  .Plot V(PB0) V(PD0)

  # ------------------------------------------------

  #include <avr/io.h>
  #include <util/delay.h>


  int x = 0xff;
  int y, yold;

  void delay_ms( unsigned short ams) {
    _delay_ms( ams >> 3);
  }

  void init( void) {
      DDRD=0xff;
      PORTD=0xff;
      DDRB=0x00;
      PORTB=0xff;
      UCSRB=0x00;
      yold = y = 0;
  }

  int main ( void) {
      init();

      while (1) {
          if (bit_is_clear(PINB, 0)) y = 0; else y = 0xff;
          if (y == 0 && yold == 0xff) {
            if (x == 0xff)
              x = 0;
            else
              x = 0xff;
          }
          yold = y;
          PORTD = x;
          delay_ms(100);
      }
  }

VMLab 3.15 RS-FlipFlop witn init switch ("Cauchy" more or less)
---------------------------------------------------------------

  D1 VDD NS0           ;
  K0 NS0 VSS           ; Stimulus: Momentary "SET"
  X0 ND2 NS0 NS0 NSET  ;

  D2 VDD NS1           ;
  K1 NS1 VSS           ; Stimulus: Momentary "RESET"
  X1 ND2 NS1 VON NRST  ;

  K3 VDD VON LATCHED   ; "Cauchy" style "INIT"
  R2 VON VSS 1K        ;
  XON ND2 VON VON NON  ;
  D7 VDD NON           ;
  D8 VDD NON           ;

  XRS0 ND2 NSET NSET NNSET
  XRS1 ND2 NRST NRST NNRST
  XRS2 ND2 NNSET NNQ NQ
  XRS3 ND2 NNRST NQ  NNQ

  XSET ND2 NQ NQ NDSET   ; Probe "Q"
  D3 VDD NDSET           ;

  XRST ND2 NNQ NNQ NDRST ; Probe "!Q"
  D4 VDD NDRST           ;

Arduino Uno (UnoArduSim v.1.3.2)
--------------------------------

  int x = HIGH;
  int y, yold;

  void setup()
  {
      pinMode( 9, OUTPUT);
      pinMode(10, OUTPUT);

      pinMode( 8, INPUT);
      pinMode( 7, INPUT);
      pinMode( 6, INPUT);
      pinMode( 5, INPUT);

      yold = y = LOW;
  }

  void loop() {
      y= digitalRead( 8) && digitalRead( 7) &&
         digitalRead( 6) && digitalRead( 5);

      if (y == LOW && yold == HIGH) {
          if (x == HIGH)
              x = LOW;
          else
              x = HIGH;
      }

      yold = y;

      digitalWrite( 9, x);
      digitalWrite(10, x);
      delay( 100);
  }

FAQ
---

+ Why not to use some 74xx nowadays?

  Each chip needs to be powered, no power consuming sleep mode built in.

+ Does AVR-8Bit survives for a longer time since now?

  Optimistic view               Pessimistic view
  ---------------------------------------------------------
  TTL is not dead since 1961    Microchip PIC vs Atmel AVR:
                                One down, one to go.*

*) https://www.fourmilab.ch/evilempire/

Ext INT for AVR links
---------------------

  AVR GUIDE

    https://sites.google.com/site/qeewiki/books/avr-guide

  EXTERNAL INTERRUPTS ON THE ATmega8

    https://sites.google.com/site/qeewiki/books/avr-guide
    /external-interrupts

  AVR external interrupts - INT0 example (ATmega8535)

    http://www.electronics-base.com/avr-tutorials/external-interrupts
         /107-avr-external-interrupts-int0-example

See Also
--------

Fritzing

  http://fritzing.org/home/

Firmata, Arduino

  https://www.arduino.cc/en/Reference/Firmata
  http://shokai.github.io/arduino_firmata/
  https://www.npmjs.com/package/arduino-firmata
  http://www.acraigie.com/programming/firmatavb/

Firmata Arduino Python

  http://playground.arduino.cc/Interfacing/Python
  http://raspberrypi-aa.github.io/session3/firmata.html
