NataliaaaaaaaaAbraham Contreras
Published

Ranma 1/2 Pin

Yet another pin using an ATtiny.

BeginnerWork in progress1.5 hours390

Things used in this project

Hardware components

ATtiny85
Microchip Technology ATtiny85
×1
Battery holder cr2032
×1
0.1uF 0805 cap
×1
100-150 Ohm 0805 Resistors
×4
slide switch smd
×1
0805 LEDs
×8
PCBWay Custom PCB
PCBWay Custom PCB
×1

Software apps and online services

Arduino IDE
Arduino IDE
Atmel Studio 7
Microchip Technology Atmel Studio 7

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

uploads2ftmp2f62e69605-a29b-490b-99b6-b03c422e7cb62fanime_hOupJSPBsB.pdf

Code

ranmaButton.ino

Arduino
Generate hex file and use Atmel ICE programmer, or use the ISP header with Arduino UNO to reprogram the ATTINY85, should work with ATTINY13 as well
//#ifdef F_CPU
//#undef F_CPU
//#define F_CPU 1000000L
//#endif

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/power.h>    // Power management

#define CHARLIE_A 0
#define CHARLIE_B 1
#define CHARLIE_C 2
#define CHARLIE_D 3

volatile int numLoop = 1;
byte randomLed = 1;
byte currLed = 1;
void setup()
{
  DDRB &= ~(1 << PB4);  //PCINT4, where a button is attached (pulled up)
  PORTB |= (1 << PB4);  //Pull up using software
   ADCSRA |= (1<<ADEN); //Enable ADC
  randomSeed (analogRead (0));    // randomize
   ADCSRA &= ~(1<<ADEN); //Disable ADC, saves ~230uA
   cli();          //Deactivate Interrupts as long as I change Bits      
  //MCUCR |= 1<<ISC01;   // INT0 mode = FALLING
  GIMSK |= (1<<PCIE);                    // Enable Pin Change Interrupts
  PCMSK |= (1 << PB4);                   // Use PB4 as interrupt pin 
  sei();          //Activate Interrupts
}

void loop() 
{
    if(numLoop>5)
         numLoop=1;


    switch(numLoop)
    {
      case (1):
        for(int i =8; i>=1; i--)
           ledSeq(i, 10*i+15);
      break;
      case (2):
        for(int i =8; i>=1; i--)
           ledSeq(i, 2*i+15);
      break;
      case (3):
         for(int i =1; i<=8; i++)
           ledSeq(i, 10*i+15);
      break;
      case (4):
         for(int i =1; i<=8; i++)
           ledSeq(i, 2*i+15);
      break;
      case (5):
      for (int j= 1; j<=20; j++)
      {
         for(int i=8; i>=1; i--)
         {
          randomLed = random (0,8);
          if (currLed != i)
          { 
            ledSeq(randomLed, 50*i+30);
             currLed = i;
          }
          else 
          {
              if (i>=8)
              ledSeq(randomLed-1, 50*i+30); 
              else
                   ledSeq(randomLed+1, 50*i+30); 
          }  
          //delay(3);
          }
      }

      pinMode(CHARLIE_A, INPUT);      //row 4
      pinMode(CHARLIE_B, INPUT);      //row 4
      pinMode(CHARLIE_C, INPUT);      //row 4
      pinMode(CHARLIE_D, INPUT);      //row 4
       goToSleep();
       break;
      }
}


ISR(PCINT0_vect)
{
  //Since the PCINTn triggers on both rising and falling edge let's just looks for rising edge
  //i.e. pin goes to 5v
//  byte pinState;
//  pinState = (PINB >> PB4)& 1; //PINB is the register to read the state of the pins
//  if (pinState <1) //look at the pin state on the pin PINB register- returns 1 if high
//    numLoop++;
static unsigned long last_interrupt_time = 0;
 unsigned long interrupt_time = millis();
 // If interrupts come faster than 200ms, assume it's a bounce and ignore
 if (interrupt_time - last_interrupt_time > 200)
 {
   numLoop++;
 }
 last_interrupt_time = interrupt_time;
}


void goToSleep ()
 {
 set_sleep_mode (SLEEP_MODE_PWR_DOWN);
 ADCSRA = 0;            // turn off ADC
 power_all_disable ();  // power off ADC, Timer 0 and 1, serial interface
 noInterrupts ();       // timed sequence coming up
 //resetWatchdog ();      // get watchdog ready
 sleep_enable ();       // ready to sleep
 interrupts ();         // interrupts are required now
 sleep_cpu ();          // sleep                
 sleep_disable ();      // precaution
 power_all_enable ();   // power everything back on
 
 }  // end of goToSleep

 
void ledSeq(byte i, int timeOn) {
     switch(i) 
     {
      case 1:
    
        // LIGHT D1 
      //turn on LED L1
      pinMode(CHARLIE_A, INPUT);      //row 1
      digitalWrite(CHARLIE_A, LOW);
      pinMode(CHARLIE_B, OUTPUT);     //row 2
      digitalWrite(CHARLIE_B, LOW);  
      pinMode(CHARLIE_C, OUTPUT);     //row 3
      digitalWrite(CHARLIE_C, HIGH);
      pinMode(CHARLIE_D, INPUT);      //row 4
      digitalWrite(CHARLIE_D, LOW);
      delay(timeOn);
      break;

      case 2:

      // LIGHT D2 
      //turn on LED L2
      pinMode(CHARLIE_A, OUTPUT);     //row 1 
      digitalWrite(CHARLIE_A, LOW);
      pinMode(CHARLIE_B, OUTPUT);     //row 2
      digitalWrite(CHARLIE_B, HIGH);   
      pinMode(CHARLIE_C, INPUT);      //row 3
      digitalWrite(CHARLIE_C, LOW);
      pinMode(CHARLIE_D, INPUT);      //row 4
      digitalWrite(CHARLIE_D, LOW);
      delay(timeOn);
      break;
//
//      // LIGHT D3
     case 3:
      pinMode(CHARLIE_A, OUTPUT);     //row 1
      digitalWrite(CHARLIE_A, HIGH);
      pinMode(CHARLIE_B, OUTPUT);     //row 2
      digitalWrite(CHARLIE_B, LOW);  
      pinMode(CHARLIE_C, INPUT);      //row 3
      digitalWrite(CHARLIE_C, LOW);
      pinMode(CHARLIE_D, INPUT);      //row 4
      digitalWrite(CHARLIE_D, LOW);
      delay(timeOn);
      break;

     
//    // LIGHT D4 
    case 4:
    pinMode(CHARLIE_A, INPUT);        //row 1
    digitalWrite(CHARLIE_A, LOW);
    pinMode(CHARLIE_B, OUTPUT);       //row 2
    digitalWrite(CHARLIE_B, HIGH);  
    pinMode(CHARLIE_C, OUTPUT);       //row 3
    digitalWrite(CHARLIE_C, LOW);
    pinMode(CHARLIE_D, INPUT);        //row 4
    digitalWrite(CHARLIE_D, LOW);
     delay(timeOn);
     break;
//    
//
//    // LIGHT D5 
    case 5:
    pinMode(CHARLIE_A, OUTPUT);       //row 1
    digitalWrite(CHARLIE_A, LOW);
    pinMode(CHARLIE_B, INPUT);        //row 2
    digitalWrite(CHARLIE_B, LOW);
    pinMode(CHARLIE_C, OUTPUT);       //row 3
    digitalWrite(CHARLIE_C, HIGH);
    pinMode(CHARLIE_D, INPUT);        //row 4
    digitalWrite(CHARLIE_D, LOW);
    delay(timeOn);
    break;
//
//    // LIGHT D6 
    case 6:
    pinMode(CHARLIE_A, OUTPUT);
    digitalWrite(CHARLIE_A, HIGH);
    pinMode(CHARLIE_B, INPUT);
    digitalWrite(CHARLIE_B, LOW);
    pinMode(CHARLIE_C, OUTPUT);
    digitalWrite(CHARLIE_C, LOW);
    pinMode(CHARLIE_D, INPUT);       //row 3
    digitalWrite(CHARLIE_D, LOW);
     delay(timeOn);
     break;
//
//    // LIGHT D7 
    case 7:
    pinMode(CHARLIE_A, INPUT);
    digitalWrite(CHARLIE_A, LOW);
    pinMode(CHARLIE_B, INPUT);
    digitalWrite(CHARLIE_B, LOW);
    pinMode(CHARLIE_C, OUTPUT);
    digitalWrite(CHARLIE_C, LOW);
    pinMode(CHARLIE_D, OUTPUT);       //row 3
    digitalWrite(CHARLIE_D, HIGH);
    delay(timeOn);
    break;
//
//    
//    // LIGHT D8 
    case 8:
    pinMode(CHARLIE_A, INPUT);
    digitalWrite(CHARLIE_A, LOW);
    pinMode(CHARLIE_B, INPUT);
    digitalWrite(CHARLIE_B, LOW);
    pinMode(CHARLIE_C, OUTPUT);
    digitalWrite(CHARLIE_C, HIGH);
    pinMode(CHARLIE_D, OUTPUT);    //row 3
    digitalWrite(CHARLIE_D, LOW);
    delay(timeOn);
    break;
 }
}

Credits

Nataliaaaaaaaa

Nataliaaaaaaaa

23 projects • 23 followers
Electrical Engineer with a particular interest in cute electronic projects.
Abraham Contreras

Abraham Contreras

16 projects • 18 followers
Applications engineer and model scale maker
Thanks to Nick Gammon.

Comments