CMS
- 1 ответ
- 0 вопросов
1
Вклад в тег
int txPin = 9; // pin connected to RF transmitter
int i; // counter to send command pulses
int pulse; // count pulse repetitions
int incomingByte = 0; // for incoming serial data
// hard coded commands (see txButton): 1 - pulse start, 2 - zero, 3 - one, 4 - pause, 5 - low
int button1[45]={44, 1, 2, 4, 2, 4, 2, 4, 3, 5, 2, 4, 2, 4, 3, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2};
int button2[43]={43, 1, 2, 4, 2, 4, 2, 4, 3, 5, 2, 4, 2, 4, 3, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 5, 3, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2};
int button3[41]={40, 1, 2, 4, 2, 4, 2, 4, 3, 5, 2, 4, 2, 4, 3, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 5, 3, 5, 3, 4, 2, 4, 2, 4, 2};
int button4[43]={42, 1, 2, 4, 2, 4, 2, 4, 3, 5, 2, 4, 2, 4, 3, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 5, 3, 4, 2, 4, 2, 4, 2};
int button5[43]={42, 1, 2, 4, 2, 4, 2, 4, 3, 5, 2, 4, 2, 4, 3, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 5, 2, 4, 3, 4, 2, 4, 2, 4, 2, 4, 2};
int button6[43]={42, 1, 2, 4, 2, 4, 2, 4, 3, 5, 2, 4, 2, 4, 3, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 5, 3, 4, 2, 4, 2, 4, 2, 4, 2};
int button7[41]={40, 1, 2, 4, 2, 4, 2, 4, 3, 5, 2, 4, 2, 4, 3, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 5, 3, 4, 2, 5, 3, 4, 2, 4, 2};
int button8[43]={42, 1, 2, 4, 2, 4, 2, 4, 3, 5, 2, 4, 2, 4, 3, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 5, 3, 4, 2, 4, 2};
int button9[43]={42, 1, 2, 4, 2, 4, 2, 4, 3, 5, 2, 4, 2, 4, 3, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 5, 2, 4, 2, 4, 3, 4, 2, 4, 2, 4, 2};
int button10[43]={42, 1, 2, 4, 2, 4, 2, 4, 3, 5, 2, 4, 2, 4, 3, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 5, 2, 4, 3, 4, 2, 4, 2, 4, 2};
int button11[41]={40, 1, 2, 4, 2, 4, 2, 4, 3, 5, 2, 4, 2, 4, 3, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 5, 3, 4, 2, 5, 2, 4, 3, 4, 2};
void setup () {
pinMode(txPin, OUTPUT);
Serial.begin(9600);
Serial.println("Number = button; a to press 0; b to shut off all");
}
void loop(){
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
switch(incomingByte) {
case 49:
txButton(button1);
Serial.println("Switching on 1");
break;
case 50:
txButton(button2);
Serial.println("Switching on 2");
break;
case 51:
txButton(button3);
Serial.println("Switching on 3");
break;
case 52:
txButton(button4);
Serial.println("Switching on 4");
break;
case 53:
txButton(button5);
Serial.println("Switching on 5");
break;
case 54:
txButton(button6);
Serial.println("Switching on 6");
break;
case 55:
txButton(button7);
Serial.println("Switching on 7");
break;
case 56:
txButton(button8);
Serial.println("Switching on 8");
break;
case 57:
txButton(button9);
Serial.println("Switching on 9");
break;
case 97:
txButton(button10);
Serial.println("Switching on 0");
break;
case 98:
txButton(button11);
Serial.println("Switching All off");
break;
}
} // end if serial available
}// end void loop
// transmit command. Due to transmitter (or something, I don't know) transmission code should be INVERTED. Ex: one is coded as LOW-delay->HIGH instead of HIGH-delay-LOW
void txButton(int cmd[]) {
Serial.print("Processing. Array size is ");
Serial.println(cmd[0]);
digitalWrite(txPin, HIGH); // not sure if its required, just an attempt to start transmission to enable AGC of the receiver
delay(1000);
for (pulse= 0; pulse <= 100; pulse=pulse+1) { // repeat command 100 times
for (i = 1; i < cmd[0]+1; i = i + 1) { // transmit command
switch(cmd[i]) {
case 1: // start
digitalWrite(txPin, HIGH);
delayMicroseconds(550);
digitalWrite(txPin, LOW);
// Serial.print("s");
break;
case 2: // "zero", that is short high spike
digitalWrite(txPin, LOW);
delayMicroseconds(110);
digitalWrite(txPin, HIGH);
// Serial.print("0");
break;
case 3: // "one", that is long high spike
digitalWrite(txPin, LOW);
delayMicroseconds(303);
digitalWrite(txPin, HIGH);
// Serial.print("1");
break;
case 4: // pause, that is short low spike
digitalWrite(txPin, HIGH);
delayMicroseconds(110);
digitalWrite(txPin, LOW);
// Serial.print("p");
break;
case 5: // low, that is long low spike
digitalWrite(txPin, HIGH);
delayMicroseconds(290);
digitalWrite(txPin, LOW);
// Serial.print("l");
break;
}
}
}
}