Go Down

Topic: digital read/write not working within switch/case serial loop (Read 1 time) previous topic - next topic

flotilla

Quote
You need to add a Serial.print() statement to loop, to print out the value in mode.


Thanks, can you explain to me where in the loop and what this would achieve?

If it is just to monitor the state of the button I can't actually view the serial port output as the app does not support it. Or is this to output the button state to the serial port to allow it to be read by the program?

I know the switch works because If I hold it whilst the key is pressed the mode does change (as indicated by the test LED lighting up after 3 seconds)

Sorry but I am trying to understand why this would work rather than 'it works'

Thanks

flotilla

The way I understand it my code is operating as the following at the moment:

If Serial bytes received, read bytes and change mode based on the byte value. Only read the button value if serial data is being received.

What I need it to do is:

If serial bytes received, read bytes and change mode based on the byte value. when mode 6 is running, monitor StopPin and if value goes high change mode to 7.

I am having trouble finding exactly where in my code I need to put the monitor to allow me to switch the mode without simultaneously receiving data from the serial port.

PaulS

Quote
Thanks, can you explain to me where in the loop and what this would achieve?

Just before the switch(mode) statement would be good. You'd KNOW what state the code is in, instead of guessing/hoping/wondering.

Quote
If it is just to monitor the state of the button I can't actually view the serial port output as the app does not support it.

What app doesn't support "it"?

Quote
Or is this to output the button state to the serial port to allow it to be read by the program?

It is to output to the serial port the mode (not the state of a switch). Read by what program? What do you have on the PC end of the serial port?

flotilla

#13
Dec 10, 2012, 08:44 pm Last Edit: Dec 10, 2012, 08:46 pm by flotilla Reason: 1
Sorry, more information. I am using a Bluetooth module attached to the arduino. I am using an android app which when you set up a touchbutton and press it, it sends two bytes of information (key,val) over bluetooth to the serial port. This is all working great and triggers the right pins when the right touch button is pressed.

Because the app does not have a serial monitor I cannot see what is going on, the only way I know is by using LEd's on the pins to monitor the mode changes.  Using the PC serial monitor as well as the BT module does not work as I think the BT module is the serial input/output port. (I am a beginner so I am still working all this out)

I know that there is no problem with monitoring the switch input because It does switch the mode if I am already making the StopPin HIGH when the mode starts (if I hold down the StopPin button whilst pressing the button on the app).  

I know this is because I have the mode changes within the IF serial.available statement and changes can only be made at the same time as serial information is being recieved. I cannot work out a way to have the StopPin change the mode, within the Switch/case block whilst Mode 6 is already running, without having to recieve serial data at the same time.

Sorry if this is confusing as hell

flotilla

Ok,
    solved this, thanks for your help PaulS but I think part of my problem was explaining my problem. After many an hour head scratching
I have solved it.

My problem was having the switch/case inside the if (serial.available(){ so monitoring of the input could only happen whilst it was receiving serial data.

I moved it outside of the IF statement so that all the above did was write the values to key and val and it worked a charm

void loop() {

if( Serial.available() >= 2){
    key = Serial.read();
    val = Serial.read(); 
}

    if (key == 4 && val == 1){ mode = 1;}
    if (key == 5 && val == 1){ mode = 2;}
    if (key == 13 && val == 1){ mode = 3;}
   

    switch (mode){

Thanks again for help

Go Up