I'm trying to squeeze code onto an ATtiny10, but I'm just not getting there. I'm using 1060 bytes and only have space for 1024 bytes.
The code is simple enough; I need to read a button pin. If high it needs to pick a random value and that causes one of two random LEDs on the PCB to turn on for two seconds. Is there a way to optimize this code to work on this IC?
int buttonState = 0;
void setup() {
pinMode(3, INPUT);
}
void loop() {
buttonState = digitalRead(3);
if (buttonState == HIGH) {
bool ranNum=random(0,1);
if(ranNum == 0) {
digitalWrite(0, HIGH);
}
else {
digitalWrite(1, HIGH);
}
delay(2000);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
}
}
digitalWrite(),digitalRead()andpinMode(). When not using this the compiler might optimize these functions out. – chrisl Feb 03 '22 at 14:42if(ranNum == 0) { digitalWrite(0, HIGH); } else { digitalWrite(1, HIGH); }intodigitalWrite(ranNum == 0 ? 0 : 1, HIGH);– Michel Keijzers Feb 03 '22 at 14:45digitalWrite(ranNum & 1, HIGH)-- also use auint8_tinstead of anint. – Majenko Feb 03 '22 at 14:57random()is a wrapper aroundrand()which probably wastes bytes. Userand() & 1to get a random 1 or 0. – Majenko Feb 03 '22 at 14:58pinMode(3,INPUT)seems like the default, and the random HIGHs on 0&1 seem inconsistent with the LOWS on 1&2 – Dave X Feb 03 '22 at 15:38