What does 54, 55 and 56 represent? Those are ASCII codes for '6', '7', '8'? Then just use those constants in your code instead of the magic numbers.
if(sread=='6') {
Your return message is one character and it's 0, 1, 3 or 9. Those are not printable characters so it will appear as if nothing is returned to the serial monitor. You could return '0', '1', '3' or '9' instead and those would be able to be shown in the serial monitor.
Please put your code in its own window as seen in other posts. This can be done by placing [code] and [/code] around the code or use the </> icon. This makes it easier for others to read.
(i make all in picture,but instead led - rele 12v 5A)
I chaged code - work with port in one way:
(if i am not shipping port, it work 50\50, but i can restart usb port by "devcon ..." and work next after restart...)
int btnSost=LOW;
char send_mess;
byte flag;
void setup() {
pinMode(12, OUTPUT);
Serial.begin(9600);
Serial.setTimeout(250);
pinMode(2,INPUT);
digitalWrite(12, HIGH);
}
void loop() {
if (flag==0){
btnSost=digitalRead(2);
send_mess='0';
if (btnSost==HIGH){send_mess='1';}
}else{
Serial.println(send_mess);
flag=0;
}
delay(600);
}
void serialEvent(){
if (Serial.available()>0){
char sread=Serial.read();
switch (sread){
case '6': //openCash
send_mess='0';
if (btnSost==HIGH){send_mess='1';}
break;
case '7': //init;
send_mess='3';
break;
case '8': //open
if (btnSost==HIGH){
digitalWrite (12,LOW);
delay(200);//open rele
digitalWrite (12,HIGH);
//delay(300);
send_mess='9';
} else {send_mess='2';}
break;
}
flag=1;
}
}
Picture shematic - example!
Problem in pin = 12
Mechanic work! but not callback!
I think problem not in my device or arduino!
Maybe in timeout (delay)!
(Reliability programs directly proportional to the time delay)
I play around with a couple of hours, all without digitalwrite() ....
Don't do that. That locks up your whole program for more than half a second. It only processes characters one at a time so if you send "88888" then it will take a few seconds to finish reading all of those eights.
The documentation for serialEvent() says that it's like an interrupt but serialEvent() only gets called after loop() finishes. It's not normally used by advanced programmers.
The way you have it in your schematic is not how it is wired up! The schematic shows pin 12 unconnected. It shows pin 13 connected to an LED but your descriptions are talking about a cash drawer? Please just draw the schematic; don't use Fritzing.
Ok Thanks, i am bad in painting...
my shematic(button work great, and coil work 50\50, but have not callback,BUT IF USB FROZEN --> I CAN IT RESTART AND WORK GO!)