Iam making an awesome christmas present a custom pcb badge. Everything works great with hooked up uno, but its heart is actually a Attiny13a, where I get ONLY around 1024bytes (mine code has 1944) 64bytes of variabels (I somehow managed to make few things copy on run so i barely get to it at 59 bytes)
The thing is how can I make this even smaller??
void setup() {
pinMode(3, INPUT_PULLUP);
}
void charlie(int a[])
{
if(a[0] < 0){
pinMode(5, INPUT);
}else
{
pinMode(5, OUTPUT);
digitalWrite(5, a[0]);
}
if(a[1] < 0){
pinMode(6, INPUT);
}else
{
pinMode(6, OUTPUT);
digitalWrite(6, a[1]);
}
if(a[2] < 0){
pinMode(2, INPUT);
}else
{
pinMode(2, OUTPUT);
digitalWrite(2, a[2]);
}
if(a[3] < 0){
pinMode(4, INPUT);
}else
{
pinMode(4, OUTPUT);
digitalWrite(4, a[3]);
}
}
void copy(int* src, int* dst, int len) {
memcpy(dst, src, sizeof(src[0])*len);
}
void resetP()
{
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(2, INPUT);
pinMode(4, INPUT);
}
void loop() {
int koz1[4] = {-2,LOW,HIGH,-2};
//int koz2[4] = {-2,HIGH,LOW,-2};
int koz2[4];
copy(koz1, koz2, 4);
koz2[1] = HIGH;
koz2[2] = LOW;
int koz3[4] = {LOW,HIGH,-2,-2};
//int koz4[4] = {HIGH,LOW,-2,-2};
int koz4[4];
copy(koz3, koz4, 4);
koz4[0] = HIGH;
koz4[1] = LOW;
int koz5[4] = {LOW, -2,-2,HIGH};
//int koz6[4] = {HIGH, -2,-2,LOW};
int koz6[4];
copy(koz5, koz6, 4);
koz4[0] = HIGH;
koz4[3] = LOW;
int cap[4] = {-2, HIGH,-2,LOW};
int oko1[4] = {LOW, -2,HIGH,-2};
//int oko2[4] = {HIGH, -2,LOW,-2};
int oko2[4];
copy(oko1, oko2, 4);
oko2[0] = HIGH;
oko2[2] = LOW;
// put your main code here, to run repeatedly:
static long oldTime = 0;
long Ctime = millis();
long diffT = Ctime - oldTime;
oldTime = Ctime;
static bool holdButt = false;
static long holdTime = 0;
static long shortPressATime = 0;
//static long longPressATime = 0;
static bool kozichOn = true;
//charlie(oko1);
//charlie(oko2);
if(digitalRead(3) == LOW)
{
holdButt = true;
holdTime = holdTime + diffT;
}
else if (holdButt && digitalRead(3) == HIGH){
holdButt = false;
if(holdTime > 1000){
//longPressATime = 5000;
kozichOn = !kozichOn;
}
else
{
shortPressATime = 5000;
}
holdTime = 0;
}
if(shortPressATime > 0)
{
shortPressATime = shortPressATime - diffT;
/*charlie(koz1);
charlie(koz2);
charlie(koz3);
charlie(koz4);
charlie(koz5);
charlie(koz6);
charlie(cap);
*/
if(((round(shortPressATime/100) / 2) & 1) == 0){
charlie(oko1);
charlie(oko2);
resetP();
}
else{
}
}
/*
else if (longPressATime > 0)
{
longPressATime = longPressATime - diffT;
charlie(oko1);
delay(5000/longPressATime);
charlie(oko2);
delay(5000/longPressATime);
longPressATime = longPressATime - 5000/longPressATime;
longPressATime = longPressATime - 5000/longPressATime;
}*/
else{
if(kozichOn){
charlie(koz1);
charlie(koz2);
charlie(koz3);
charlie(koz4);
charlie(koz5);
charlie(koz6);
charlie(cap);
}
charlie(oko1);
charlie(oko2);
}
resetP();
}
EDIT: Guys you are awesome together with those on arduino forums it looks like I finally achieved it, I will try to optimize little bit more as its 1010bytes 98%. But I thank you all. You teached me in 1 hour more practical things, than I could read in a week.
void setup() {
pinMode(3, INPUT_PULLUP);
}
void charlie(byte a[])
{
if(a[0] == 0x0F){
pinMode(5, INPUT);
}else
{
pinMode(5, OUTPUT);
digitalWrite(5, a[0]);
}
if(a[1] == 0x0F){
pinMode(6, INPUT);
}else
{
pinMode(6, OUTPUT);
digitalWrite(6, a[1]);
}
if(a[2] == 0x0F){
pinMode(2, INPUT);
}else
{
pinMode(2, OUTPUT);
digitalWrite(2, a[2]);
}
if(a[3] == 0x0F){
pinMode(4, INPUT);
}else
{
pinMode(4, OUTPUT);
digitalWrite(4, a[3]);
}
}
/*
void copy(int* src, int* dst, int len) {
memcpy(dst, src, sizeof(src[0])*len);
}
*/
/*
void resetP()
{
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(2, INPUT);
pinMode(4, INPUT);
}
*/
void loop() {
byte koz1[4] = {0x0F,LOW,HIGH,0x0F};
//int koz2[4] = {null,HIGH,LOW,null};
byte koz2[4];
//copy(koz1, koz2, 4);
memcpy(koz2, koz1, sizeof(koz1[0])*4);
koz2[1] = HIGH;
koz2[2] = LOW;
byte koz3[4] = {LOW,HIGH,0x0F,0x0F};
//int koz4[4] = {HIGH,LOW,2,2};
byte koz4[4];
//copy(koz3, koz4, 4);
memcpy(koz4, koz3, sizeof(koz3[0])*4);
koz4[0] = HIGH;
koz4[1] = LOW;
byte koz5[4] = {LOW, 0x0F,0x0F,HIGH};
//int koz6[4] = {HIGH, 2,2,LOW};
byte koz6[4];
//copy(koz5, koz6, 4);
memcpy(koz6, koz5, sizeof(koz5[0])*4);
koz4[0] = HIGH;
koz4[3] = LOW;
byte cap[4] = {0x0F, HIGH,0x0F,LOW};
byte oko1[4] = {LOW, 0x0F,HIGH,0x0F};
//int oko2[4] = {HIGH, 2,LOW,2};
byte oko2[4];
//copy(oko1, oko2, 4);
memcpy(oko2, oko1, sizeof(oko1[0])*4);
oko2[0] = HIGH;
oko2[2] = LOW;
// put your main code here, to run repeatedly:
static uint16_t oldTime = 0;
uint16_t Ctime = millis();
uint16_t diffT = Ctime - oldTime;
oldTime = Ctime;
static bool holdButt = false;
static uint16_t holdTime = 0;
static uint16_t shortPressATime = 0;
//static long longPressATime = 0;
static bool kozichOn = true;
//charlie(oko1);
//charlie(oko2);
if(digitalRead(3) == LOW)
{
holdButt = true;
holdTime = holdTime + diffT;
}
else if (holdButt && digitalRead(3) == HIGH){
holdButt = false;
if(holdTime > 1000){
//longPressATime = 5000;
kozichOn = !kozichOn;
}
else
{
shortPressATime = 5000;
}
holdTime = 0;
}
if(shortPressATime > 0)
{
shortPressATime = shortPressATime - diffT;
if(((uint16_t(shortPressATime/128) / 2) & 1) == 0){
charlie(oko1);
charlie(oko2);
}
else{
}
}
else{
if(kozichOn){
charlie(koz1);
charlie(koz2);
charlie(koz3);
charlie(koz4);
charlie(koz5);
charlie(koz6);
charlie(cap);
}
charlie(oko1);
charlie(oko2);
}
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(2, INPUT);
pinMode(4, INPUT);
}