Number (1-127) to control 7 IO
ok, have program on pc sends number 1 127, tell arduino pins activate. key goes this:
pin 13 = 64
pin 12 = 32
pin 11 = 16
pin 10 = 8
pin 9 = 4
pin 8 = 2
pin 7 = 1
so if pc sends 127, pins on.
i have code works first two, messy implement more. wondering if there shortcuts getting pin control information out.
pin 13 = 64
pin 12 = 32
pin 11 = 16
pin 10 = 8
pin 9 = 4
pin 8 = 2
pin 7 = 1
so if pc sends 127, pins on.
i have code works first two, messy implement more. wondering if there shortcuts getting pin control information out.
code: [select]
char serinstring[100]; // array hold different bytes of string. 100=100characters;
// -> must state how long array else won't work properly
char colorcode;
int colorval;
int x;
int outputpin = 13;
int outputpin2 = 12;
void setup() {
pinmode(outputpin, output);
pinmode(outputpin2, output);
serial.begin(9600);
}
void loop () {
//read serial port , create string out of read
readserialstring(serinstring);
colorval = atoi(serinstring);
serinstring[0] = 0;
serinstring[1] = 0;
serinstring[2] = 0;
serinstring[3] = 0;
serial.println(colorval, bin);
if (colorval - 64 >= 0) {
digitalwrite(outputpin, high);
colorval = colorval - 64;
if (colorval - 32 >= 0) {
digitalwrite(outputpin2, high);
colorval = colorval - 32;
}
else if (colorval - 32 < 0) {
digitalwrite(outputpin2, low);
}
}
else if (colorval - 64 < 0) {
digitalwrite(outputpin, low);
if (colorval - 32 >= 0) {
digitalwrite(outputpin2, high);
colorval = colorval - 32;
}
else if (colorval - 32 < 0) {
digitalwrite(outputpin2, low);
}
}
delay(100); // wait bit, serial data
}
//read string serial , store in array
//you must supply array variable
void readserialstring (char *strarray) {
int = 0;
if(!serial.available()) {
return;
}
while (serial.available()) {
strarray[i] = serial.read();
i++;
}
}
this function issues digital number between 0 , 15 on 4 io pins. believe trick you.
note : do_trigger_code_start in context first pin on issue number. it's supposed (in code) msb, use do_trigger_code_start+3-x pin number. don't need this.
note 2 : rewrote code little better fit need. haven't tested it, should work.
code: [select]
void issueevent(int code) {
int x = 0;
for (x=3; x>=0; x--) {
digitalwrite(do_trigger_code_start+3-x, ((code & int(pow(2,x))) ? high : low));
}
}
note : do_trigger_code_start in context first pin on issue number. it's supposed (in code) msb, use do_trigger_code_start+3-x pin number. don't need this.
note 2 : rewrote code little better fit need. haven't tested it, should work.
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Number (1-127) to control 7 IO
arduino
Comments
Post a Comment