In this first test I have 4 LEDS as output and 4 button switches as input.
The idea is for the PC to send the Arduino commands and the Arduino to act on them.
The command to toggle a LED is a character "1","2","3" or "4". This works as expected.
The command to return the state of the buttons as a four character string is the character "9".
When I hold down a button and press the [9] key I expected the Arduino to return a string holding the button current values. Instead it returns the previous button values.
While still holding down button1 I enter the command 9 again and this time it returns 0111 showing I am holding down button1
I release the button1 and again enter the command 9 and it still returns 0111
If I again press the command 9 it now returns the expected 1111
In other words I have to send the command twice. The first time it returns the previous state and the second time it returns the actual state.
Code: Select all
screenres 640,480,32
Open Com "COM3:9600,n,8,1,cs0,ds0,cd0,rs" As #1
If Err <> 0 Then
Print "Error opening COM3"
Sleep 2000,1
Cls
end
End If
function readButtons() as string
dim inpData as string
inpData = ":"
while Loc(1) > 0
inpData = inpData + Input(1,#1)
wend
return inpData
end function
dim as string key
dim as string buttons
do
input "Enter command";key 'enter command
if key >= "1" and key <= "4" then 'command to toggle a LED
print #1,key; 'send toggle command
end if
'before pressing [9] key hold down one of the buttons
if key="9" then 'command to read the button values
print #1,key 'send command to put button values in buffer
buttons = readButtons() 'read buffer
print buttons; 'show button values
end if
Sleep 2
loop until key="q"
Close #1
Code: Select all
int serialData = 0;
void setup(){
pinMode(22,INPUT); // button switches
pinMode(23,INPUT);
pinMode(24,INPUT);
pinMode(25,INPUT);
pinMode(30,OUTPUT); // output to LEDS
pinMode(31,OUTPUT);
pinMode(32,OUTPUT);
pinMode(33,OUTPUT);
Serial.begin(9600);
}
const int LED1 = 30;
const int LED2 = 31;
const int LED3 = 32;
const int LED4 = 33;
int LEDS[5];
int const btn1 = 22;
int const btn2 = 23;
int const btn3 = 24;
int const btn4 = 25;
String buttons;
void readButtons(){
buttons = "";
if (digitalRead(btn1)==HIGH){
buttons = buttons + '1';
}else{
buttons = buttons + '0';
}
if (digitalRead(btn2)==HIGH){
buttons = buttons + '1';
}else{
buttons = buttons + '0';
}
if (digitalRead(btn3)==HIGH){
buttons = buttons + '1';
}else{
buttons = buttons + '0';
}
if (digitalRead(btn4)==HIGH){
buttons = buttons + '1';
}else{
buttons = buttons + '0';
}
Serial.println(buttons); // place result on buffer
}
void loop(){
if (Serial.available() > 0){
serialData = Serial.read();
if (serialData == '1'){ // command to toggle LED1
if (LEDS[1]==0){
digitalWrite(LED1,HIGH);
LEDS[1]=1;
}else{
digitalWrite(LED1,LOW);
LEDS[1]=0;
}
}
if (serialData == '2'){ // command to toggle LED2
if (LEDS[2]==0){
digitalWrite(LED2,HIGH);
LEDS[2]=1;
}else{
digitalWrite(LED2,LOW);
LEDS[2]=0;
}
}
if (serialData == '3'){ // command to toggle LED3
if (LEDS[3]==0){
digitalWrite(LED3,HIGH);
LEDS[3]=1;
}else{
digitalWrite(LED3,LOW);
LEDS[3]=0;
}
}
if (serialData == '4'){ // command to toggle LED4
if (LEDS[4]==0){
digitalWrite(LED4,HIGH);
LEDS[4]=1;
}else{
digitalWrite(LED4,LOW);
LEDS[4]=0;
}
}
if (serialData == '9'){
readButtons();
}
}
}