I am completely new to programming and I am trying a brute force programming on my garage door opener. I want to use an Arduino for learning purposes. I wrote the following program that finally did what it should and now I am wondering whether there is a shorter code that does the same. Is anyone able to help me in that matter?
I do know that about 2^12 possibilities existed so I ran through all of them. I also knew that some parts of the "pulsetrain" (the set of necessary pulses) start with a particular sequence that is always the same (for all "brute forced" trains) and that the closing sequence is also always identical. I then simply wrote that down in the following, nasty code:
/*
Transmitter is connected to Arduino Pin #2
*/
void setup() {
pinMode(2, OUTPUT);
}
void pulsetrain(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k, int l, int m) {
PORTD &= ~_BV(PD2);
//set to LOW, hardwired to make it faster, see http://www.instructables.com/id/Arduino-is-Slow-and-how-to-fix-it/?ALLSTEPS
delayMicroseconds(283); // (D2)
PORTD |= _BV(PD2); //set to HIGH
delayMicroseconds(566);
PORTD &= ~_BV(PD2);
delayMicroseconds(283);
PORTD |= _BV(PD2);
delayMicroseconds(566);
PORTD &= ~_BV(PD2);
delayMicroseconds(a);
PORTD |= _BV(PD2);
delayMicroseconds(b);
PORTD &= ~_BV(PD2);
delayMicroseconds(c);
PORTD |= _BV(PD2);
delayMicroseconds(d);
PORTD &= ~_BV(PD2);
delayMicroseconds(e);
PORTD |= _BV(PD2);
delayMicroseconds(f);
PORTD &= ~_BV(PD2);
delayMicroseconds(g);
PORTD |= _BV(PD2);
delayMicroseconds(h);
PORTD &= ~_BV(PD2);
delayMicroseconds(i);
PORTD |= _BV(PD2);
delayMicroseconds(j);
PORTD &= ~_BV(PD2);
delayMicroseconds(k);
PORTD |= _BV(PD2);
delayMicroseconds(l);
PORTD &= ~_BV(PD2);
delayMicroseconds(m);
PORTD |= _BV(PD2);
delayMicroseconds(283);
PORTD &= ~_BV(PD2);
delayMicroseconds(566);
PORTD |= _BV(PD2);
delayMicroseconds(283);
PORTD &= ~_BV(PD2);
delayMicroseconds(566);
PORTD &= ~_BV(PD2); // last LOW
}
void loop() {
int signals[2] = {283, 566};
for (int a = 0; a < 2; a++) {
for (int b = 0; b < 2; b++) {
for (int c = 0; c < 2; c++) {
for (int d = 0; d < 2; d++) {
for (int e = 0; e < 2; e++) {
for (int f = 0; f < 2; f++) {
for (int g = 0; g < 2; g++) {
for (int h = 0; h < 2; h++) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
for (int l = 0; l < 2; l++) {
for (int m = 0; m < 2; m++) {
pulsetrain(signals[a], signals[b], signals[c], signals[d], signals[e], signals[f], signals[g], signals[h], signals[i], signals[j], signals[k], signals[l], signals[m]);
}
}
}
}
}
}
}
}
}
}
}
}
}
}
a
-m
would become a 13 bit number. Loop through those numbers with a for loop (0-8191). Then in the pulsetrain function read one bit, apply the delay of 283 or 566 based on the bit being 0 or 1. Then shift all the bit (left or right), and repeat (13 times). – Gerben Jan 14 '17 at 13:44