I connected the ps2Keyboard to the board for ex. i=1 is when I press button “alt” and i=0 when I press “ctrl” ! i changes the value before the while stuff …the while’s are at the and of the code …
the body are not the same…
in i==1
if (digitalRead(pPin) == HIGH) digitalWrite(ledPin,HIGH );
if (digitalRead(pPin) == LOW) digitalWrite(ledPin,LOW);}
and in i==0 I have
if (digitalRead(pPin) == HIGH) digitalWrite(ledPin,LOW );
if (digitalRead(pPin) == LOW) digitalWrite(ledPin,LOW);}
The while loop repeats until the condition is not met. In your example i is not changed within the while loop, so it will repeat forever within one of the two loops.
There is a more elegant way of doing it though. In your example, ledPin is only set as HIGH if pPin is HIGH and i is 1. You could use this:
It may be that your code could be simply reconfigured.
Don't forget that "loop" itself is in a "while(1)" loop, so your code may not need a "while" loop at all, just "if" conditionals.
#include <ps2.h>
PS2 kbd(3, 4); //3- clock; 4- DATA
int pPin = 53; //sensor pin
int ledPin = 13;
void kbd_init()
{ char ack; kbd.write(0xff); ack = kbd.read(); ack = kbd.read();}
//------------------------------------------------------
void setup()
{ kbd_init();
pinMode(pPin, INPUT);
pinMode(ledPin, OUTPUT);}
//------------------------------------------------------
void loop()
{int i=2;
unsigned char code;
for (;;) { code = kbd.read();
//--------------------------------
if (code==0x69) {i=1;}
if (code==0x72) {i=0;}
//------------------------------------------------------
switch (i) {
case 0:
digitalWrite(ledPin, LOW);
break;
case 1:
while (i!=0){
digitalWrite(ledPin, digitalRead(pPin));} //the problem is here ... a can not switch to 0 case, because in the bosy of while, the value of i is not refreshing ! is there something like echo in arduino language
break;
}}
Groove , I need to use while , because I have two states when I pres 1 on keyboard (i=1) and 0 on the keyboard(i=0) ! by presing 1, I activate my system, by pressing 0, I diactivate ...
#include <ps2.h>
PS2 kbd(3, 4); //3- clock; 4- DATA
int pPin = 53; //sensor pin
int ledPin = 13;
void kbd_init()
{ char ack; kbd.write(0xff); ack = kbd.read(); ack = kbd.read();}
//------------------------------------------------------
void setup()
{ kbd_init();
pinMode(pPin, INPUT);
pinMode(ledPin, OUTPUT);}
//------------------------------------------------------
void loop()
{int i=2;
unsigned char code;
code = kbd.read();
//--------------------------------
if (code==0x69) {i=1;}
if (code==0x72) {i=0;}
//------------------------------------------------------
switch (i) {
case 0:
digitalWrite(ledPin, LOW);
break;
case 1:
while (i!=0){
digitalWrite(ledPin, digitalRead(pPin));} //the problem is here ... a can not switch to 0 case, because in the bosy of while, the value of i is not refreshing ! is there something like echo in arduino language
break;}
}
The variable i is assigned a value based on the value returned by kbd.read(). If you never call that function again, and never re-assign i a new value, I'm at a loss to understand why you expect the value of i to change.
You are not binding i to the value output by kdb.read(), so that a change in which key is pressed automatically assigns a new value to i.
Perhaps you are misunderstanding the purpose of while, using it where you mean if. The while statement is used to do something over and over, until the termination condition is true. The if statement is used to do something once if the condition is true.