This is one of the functions in my sketch:
void packet()
{
i = 0;
while (i < 6) {
chH(); //------first part of radio packet
if (button == 'a' && state == '1') // these guys figure out which function to run
{ ha1(); }
if (button == 'a' && state == '0')
{ ha0(); }
if (button == 'b' && state == '1')
{ hb1(); }
if (button == 'b' && state == '0')
{ hb0(); }
if (button == 'c' && state == '1')
{ hc1(); }
if (button == 'c' && state == '0')
{ hc0(); }
ending(); //last part of the packet
i = i + 1;
}
}
It is used to construct a radio packet, based on variables that are set via user input from a serial connection. chH() is the first part of the packet, the ha1 ~ hc0 functions are the middle section of the packet, and ending() is the last bit.
My question is:, instead of processing all these if statements WHILE the packing is being sent, is there a way for me to predetermine which of the hxx() functions to run before it goes into the loop? Like, if (button == ‘c’ && state == ‘0’) { runFunction = hc0(); } and then just only call runFunction(); during the loop? When I tried to compile the code with it set up like the example I just wrote out, it doesn’t like it at all. Attached below is the full sketch (forgive me if it looks crazy, this is the first sketch i’ve ever written). It compiles as-is (though i haven’t had a chance to actually test it,) it just seems like there’s probably a better way of doing that one part.
Full sketch:
int transmit = 13;
int dLong = 1800;
int dShort = 600;
int dPause = 11800;
int i;
char channel;
char button = 'unset';
char state = 'unset';
char serialRead;
void setup()
{
pinMode (transmit, OUTPUT);
digitalWrite (transmit, LOW);
Serial.begin (9600);
}
//--------------------------------------------------- Channels
void packet()
{
i = 0;
while (i < 6) {
chH();
if (button == 'a' && state == '1')
{ ha1(); }
if (button == 'a' && state == '0')
{ ha0(); }
if (button == 'b' && state == '1')
{ hb1(); }
if (button == 'b' && state == '0')
{ hb0(); }
if (button == 'c' && state == '1')
{ hc1(); }
if (button == 'c' && state == '0')
{ hc0(); }
ending();
i = i + 1;
}
}
void chH () // this is the first part of the wireless packet, including (i think) which "channel set" the remote is communicating on
{
shortLong();
longShort();
longShort();
shortLong();
longShort();
shortLong();
}
void ending () // this is the last bit of the packet, which never seems to change
{
shortLong();
shortLong();
shortLong();
singleShortUp();
pause();
}
//--------------------------------------------------- Serial Stuf
void getButton ()
{ boolean gotButton = false;
Serial.println('Please select a set of buttons- a, b, or c');
while (true)
{ if (Serial.available() > 0)
{ serialRead = Serial.read();
switch (serialRead)
{ case 'a':
gotButton = true;
button = serialRead;
break;
case 'b':
gotButton = true;
button = serialRead;
break;
case 'c':
gotButton = true;
button = serialRead;
break;
default:
gotButton = false;
}
}
else
{ delay (100);
}
}
}
void getState()
{ boolean gotState = false;
Serial.println('Press 1 for on, 0 for off');
while (true)
{ if (Serial.available() > 0)
{ serialRead = Serial.read();
switch (serialRead)
{ case '1':
gotState = true;
state = serialRead;
break;
case '0':
gotState = true;
state = serialRead;
break;
default:
gotState = false;
}
}
else
{ delay (100);
}
}
}
//--------------------------------------------------- Buttons
void aOn ()
{
shortLong();
shortLong();
longShort();
shortLong();
shortLong();
shortLong();
}
void aOff()
{
shortLong();
shortLong();
shortLong();
longShort();
shortLong();
shortLong();
}
void bOn ()
{
shortLong();
shortLong();
shortLong();
shortLong();
longShort();
shortLong();
}
void bOff ()
{
shortLong();
shortLong();
shortLong();
shortLong();
shortLong();
longShort();
}
void cOn ()
{
shortLong();
longShort();
shortLong();
shortLong();
shortLong();
shortLong();
}
void cOff ()
{
longShort();
shortLong();
shortLong();
shortLong();
shortLong();
shortLong();
}
//--------------------------------------------------- Combos
void ha1 ()
{
i = 0;
while (i < 7)
{
chH();
aOn();
ending();
i = i + 1;
}
}
void ha0 ()
{
i = 0;
while (i < 7)
{
chH();
aOff();
ending();
i = i + 1;
}
}
void hb1 ()
{
i = 0;
while (i < 7)
{
chH();
bOn();
ending();
i = i + 1;
}
}
void hb0 ()
{
i = 0;
while (i < 7)
{
chH();
bOff();
ending();
i = i + 1;
}
}
void hc1 ()
{
i = 0;
while (i < 7)
{
chH();
cOn();
ending();
i = i + 1;
}
}
void hc0 ()
{
i = 0;
while (i < 7)
{
chH();
cOff();
ending();
i = i + 1;
}
}
//--------------------------------------------------- library
void shortLong ()
{
digitalWrite(transmit,HIGH);
delayMicroseconds(dShort);
digitalWrite(transmit,LOW);
delayMicroseconds(dLong);
}
void longShort ()
{
digitalWrite(transmit,HIGH);
delayMicroseconds(dLong);
digitalWrite(transmit,LOW);
delayMicroseconds(dShort);
}
void singleShortUp ()
{
digitalWrite(transmit,HIGH);
delayMicroseconds(dShort);
digitalWrite(transmit,LOW);
}
void singleLongUp ()
{
digitalWrite(transmit,HIGH);
delayMicroseconds(dLong);
digitalWrite(transmit,LOW);
}
void pause ()
{
delayMicroseconds(dPause);
}
//---------------------------------------------------
void loop()
{
getButton();
getState();
if (button != 'unset' && state != 'unset')
{ packet(); }
}
Thes purpose of this sketch is to control a set of Woods wireless RF outlets, which DO NOT have the sc2272/sc2272 chips.