Hello
This code works perfectly with my uno however I cant seem to get it to work with the spark.
This code is taken from this class that is built for arduino.
https://docs.google.com/file/d/0B0DQ7La4EBHlU2ltTmlmeHhya3M/edit
Any suggestions welcome.
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
int txPin = D5;
bool high = true;
void setup() {
pinMode(txPin, OUTPUT);
}
void loop() {
sendButton(6400, 120);
delay(5000);
}
void sendButton(unsigned int remoteID, byte keycode) {
for (int pulse= 0; pulse <= 180; pulse = pulse+1) { // how many times to transmit a command
sendPulse(1); // Start
high = true; // first pulse is always high
for (int i = 16; i>0; i--) { // transmit remoteID
byte txPulse=bitRead(remoteID, i-1); // read bits from remote ID
selectPulse(txPulse,high);
}
for (int i = 7; i>0; i--) { // transmit keycode
byte txPulse=bitRead(keycode, i-1); // read bits from keycode
selectPulse(txPulse,high);
}
}
digitalWrite(txPin, LOW);
}
// build transmit sequence so that every high pulse is followed by low and vice versa
void selectPulse(byte inBit,bool high) {
switch (inBit) {
case 0:
for (byte ii=1; ii<3; ii++) {
if (high == true) { // if current pulse should be high, send High Zero
sendPulse(2);
} else { // else send Low Zero
sendPulse(4);
}
high=!high; // invert next pulse
}
break;
case 1: // if current pulse should be high, send High One
if (high == true) {
sendPulse(3);
} else { // else send Low One
sendPulse(5);
}
high=!high; // invert next pulse
break;
}
}
// transmit pulses
// slightly corrected pulse length, use old (commented out) values if these not working for you
void sendPulse(byte txPulse) {
switch(txPulse) { // transmit pulse
case 1: // Start
digitalWrite(txPin, HIGH);
delayMicroseconds(500); // 550
digitalWrite(txPin, LOW);
break;
case 2: // "High Zero"
digitalWrite(txPin, LOW);
delayMicroseconds(100); // 110
digitalWrite(txPin, HIGH);
break;
case 3: // "High One"
digitalWrite(txPin, LOW);
delayMicroseconds(300); // 303
digitalWrite(txPin, HIGH);
break;
case 4: // "Low Zero"
digitalWrite(txPin, HIGH);
delayMicroseconds(100); // 110
digitalWrite(txPin, LOW);
break;
case 5: // "Low One"
digitalWrite(txPin, HIGH);
delayMicroseconds(300); // 290
digitalWrite(txPin, LOW);
break;
}
}