Welcome to Laser Pointer Forums - discuss green laser pointers, blue laser pointers, and all types of lasers

LPF Donation via Stripe | LPF Donation - Other Methods

Links below open in new window

ArcticMyst Security by Avery

Anyone familiar with Attiny13 programming?

rhd

0
Joined
Dec 7, 2010
Messages
8,475
Points
0
Dramatically rewrote the code, and it works great! I even added a rapid cycling mode.

If there's any problem (and I can live with this for now), it's that there's piss poor entropy to the randomness approach (probably because storing a value of 1-255 to use as the random seed just doesn't produce the needed entropy). I can detect the patterns.

Code:
#include <EEPROM.h> // include the EEPROM Memory library
boolean pinOptions[8][3] = { // Array of pin combinations (1 = on, 0 = off)
  {1,1,1},
  {1,0,0},
  {1,1,0},
  {0,1,0},
  {0,1,1},
  {0,0,1},
  {1,0,1},
  {0,0,0}
};

byte ButtonPin = 3; // see pinout
byte ColorOnePin = 0; // see pinout
byte ColorTwoPin = 1; // see pinout
byte ColorThreePin = 2; // see pinout
byte MemVar;
byte RandVar;
byte MemoryLocation = 1;
byte RandLocation = 0;
byte ButtonPinState;
byte ColorChoice;

void setup (){ // this subroutine runs once upon startup
  pinMode (0, OUTPUT); // set pin 0 to output
  pinMode (1, OUTPUT); // set pin 1 to output
  pinMode (2, OUTPUT); // set pin 2 to output
  pinMode (3, INPUT); // set pin 3 to input
  // Read memvar
  MemVar = 1;
  MemVar = EEPROM.read(MemoryLocation);
  if (MemVar > 9){ 
     MemVar = 1; // 
  }
  if (MemVar == 8){
    RandVar = EEPROM.read(RandLocation);
    randomSeed(RandVar);
    setRandomColor(); 
  }
  else if (MemVar == 9){
    RandVar = EEPROM.read(RandLocation);
    randomSeed(RandVar);
    ColorChoice = random(2, 7);
    setColor(); 
  }
  else
  {
    ColorChoice = MemVar;
    setColor();
  }
}

void loop(){ // this subroutine runs continuously after setup finishes
  ButtonPinState = digitalRead(ButtonPin);  // read ButtonPin to ButtonPinState  
  if (ButtonPinState == HIGH){ // when ButtonPinState is HIGH...
    ++ MemVar; // increment memory variable
    if (MemVar > 9){ // on 9th button press...
      MemVar = 1; // reset color
    }
    EEPROM.write(MemoryLocation, MemVar);
    // Normal mode for colors 1 through 7
    if (MemVar < 8){
      ColorChoice = MemVar;
      setColor();
      delay(300);
    } 
    // Random color mode (no cycling)
    else if (MemVar == 8){
      blinkAll();
      randomSeed(millis());
      setRandomColor();      
    }
    // Random color mode (rapid cycling) - Indicate that we're entering the mode
    else if (MemVar == 9){
      blinkAll();
    }
  }
  // Random color mode (rapid cycling) 
  if (MemVar == 9){
      randomSeed(millis());
      ColorChoice = random(2, 7);
      setColor();
      delay(50);
  }
}

void blinkAll(){
  for (int i=0; i <= 4; i++){
    ColorChoice = 8;
    setColor();
    delay(20);
    ColorChoice = 1;
    setColor();
    delay(20);      
  }
}

void setRandomColor(){
  ColorChoice = random(2, 7);
  setColor(); 
  RandVar = random(1, 255);
  EEPROM.write(RandLocation, RandVar);  
}

void setColor(){
  digitalWrite(ColorOnePin, pinOptions[ColorChoice-1][0]);
  digitalWrite(ColorTwoPin, pinOptions[ColorChoice-1][1]);
  digitalWrite(ColorThreePin, pinOptions[ColorChoice-1][2]);  
}
 
Last edited:





Joined
Dec 11, 2011
Messages
4,364
Points
83
Code looks great! It's nice to see some of my old "clunking" code made it into the final build, hehe. =P In all seriousness though there's some genius solutions in there, excellent work.

Btw, can you link which usb programmer you went with in the end? I might pick one up to save me the trouble of having to pull my arduino from use every time I need to flash an attiny.

Oh, when you finish this build fully, I really think you NEED to submit it to a DIY/Hacker magazine/blog/etc. It's visually and technically stunning enough to really bring in recognition and amazement.

edit: Oh and it looks like you got the eeprom read/write lockup fixed. Did you figure out what the problem was? Probably was it just being initially read as 0, eh?
 
Last edited:

rhd

0
Joined
Dec 7, 2010
Messages
8,475
Points
0
Got some smooth fading color cycling, without hardware PWM :)

Will try to post a video.

Code:
byte ColorRED = 0; 
byte ColorGREEN = 1; 
byte ColorBLUE = 2;
byte StageCount = 20;
byte PWMCount = 1;
byte ColorFade;

void setup (){
  pinMode (0, OUTPUT); 
  pinMode (1, OUTPUT); 
  pinMode (2, OUTPUT); 
  pinMode (3, INPUT); 

  // Fade in RED
  ColorFade = ColorRED;
  FadeIn();
}

void loop(){ 
  ColorCycle();
}

void ColorCycle(){
  ColorFade = ColorBLUE;
  FadeIn();
  ColorFade = ColorRED;
  FadeOut();
  ColorFade = ColorGREEN;
  FadeIn();
  ColorFade = ColorBLUE;
  FadeOut();  
  ColorFade = ColorRED;
  FadeIn();
  ColorFade = ColorGREEN;
  FadeOut();    
}

void FadeIn(){  
  for (int j=0; j <= StageCount; j++){
    for (int i=0; i <= PWMCount-1; i++){
      digitalWrite(ColorFade, 1);
      delay(j);
      digitalWrite(ColorFade, 0);
      delay(StageCount+1-j);
    }
  }
  digitalWrite(ColorFade, 1);
}

void FadeOut(){  
  for (int j=0; j <= StageCount; j++){
    for (int i=0; i <= PWMCount-1; i++){
      digitalWrite(ColorFade, 1);
      delay(StageCount+1-j);
      digitalWrite(ColorFade, 0);
      delay(j);
    }
  }
  digitalWrite(ColorFade, 0);
}
 
Last edited:




Top