Need help!!! :)

Have some code:

int btnSost=LOW;
int sread=0;
int send_mess=-1;

void setup() {
pinMode(12, OUTPUT);
Serial.begin(9600);
Serial.setTimeout(1000);
pinMode(2,INPUT);
digitalWrite(12, HIGH);
}
void loop() {
//delay(300);
//serialEvent();
}

void serialEvent(){
if (Serial.available()>0){
//delay(200);
sread=Serial.read();//7-init,8-open,6-cash open? ← klavishi
//Serial.flush();

if (sread==54 || sread==55 || sread==56){

if (sread==54){
//openCash
btnSost=digitalRead(2);
send_mess=0;
if (btnSost==HIGH){send_mess=1;}
}

if (sread==55){
//init
send_mess=3;
}

if( sread==56){
** //open**
** digitalWrite (12,LOW);**
** delay(200);**
** digitalWrite (12,HIGH);**
** delay(200);**
** send_mess=9;**
** }**

//out →
Serial.println(send_mess);
//delay(250);
}//||
}

}

Question:

Why usb port frozen? (up faster call DigitalWrite)
I have Arduino Uno!
P.S. sorry fo my bad english !!!

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.

send_msg = '9';

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.

How to use this forum

Weedpharma

You may find the examples in Serial Input Basics useful.

...R

Thanks to MorganS, this is changed code, but if i send ‘8888888888’ have a problem!!! HELP!!!

int btnSost=LOW;
char send_mess;

void setup() {
  pinMode(12, OUTPUT);
  Serial.begin(9600);
  Serial.setTimeout(1000);
  pinMode(2,INPUT);
  digitalWrite(12, HIGH);   
}

void loop() {}

void serialEvent(){
 if (Serial.available()>0){
  char sread=Serial.read();
  btnSost=digitalRead(2);
  
  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);
                 digitalWrite (12,HIGH); 
                 delay(300);
                 send_mess='9';
               } else {send_mess='2';} 
               break;                        
  }
      
  
  Serial.println(send_mess);
    
 }
}

And thanks to all who answer!

wrdArduino: Thanks to MorganS, this is changed code, but if i send '8888888888' have a problem!!! HELP!!!!!!!

What problem?

Serial answer '2222........... and frozen!!!

What do you expect, or want, it to send? What is this program supposed to do?

Problem: I send '8' (btnSost==LOW) and i can not get '9'!!!

Programm open Cash Drawer by audio jack + arduino (and button in Cash Drawer as sensor -> open\close) with led it work? with drawer not!

What is connected to pin 2 and how is it connected?

Button connected through a resistor to arduino ???, everything is good , the circuitry is working 100% . Checks for other models !

"Through" a resistor? That sounds wrong. Can you draw the schematic? A pencil sketch photographed with a phone is sufficient.

Shematic: https://parshev.files.wordpress.com/2011/11/button_bb1.png (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() .... :)

This delay?

  delay(600);

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!)

And I take away later delay in ,thanks!

:o

Thanks to all, problem solved! Shematic good, coding simple!!! :) from SerialEvent i removed DELAY and changed code!

Thanks to "experts" for help !

wrdArduino: Problem: I send '8' (btnSost==LOW) and i can not get '9'!!!

Answer:

case '8':   //open
               if (btnSost==HIGH){      [color=red] HERE ! ==HIGH not ==LOW [/color]
                 digitalWrite (12,LOW);
                 delay(200);
                 digitalWrite (12,HIGH);
                 delay(300);
                 send_mess='9';
               } else {send_mess='2';}
               break;