Serial Reading

Hi! I'm currently making a project that will be connecting visual studio and arduino but I have some problems with the serial reading.

void setup(){
  Serial.begin(9600);
  pinMode(2, INPUT);
  pinMode(13,OUTPUT);
  
}

void loop (){
while (Serial.available() == 0);
int val = Serial.read() - '0';

switch(val)
{
case 1: 
  digitalWrite(13, HIGH);
  //Serial.println("Ready to take in");
  if(digitalRead(2)==HIGH)
  {
    Serial.println(12);
  }

}
Serial.flush();
}

My problem is that when my arduino receives a value of 1, it turns on pin 13 but when I press the tact switch, it will not be processed. If I removed the if part (for button checking) it is able to print the value of 12 and be read at the visual studio.

for checking at the moment I am only throwing the values via the serial monitor in arduino.

Thanks!

If you send a '1' nothing is returned if pin 2 is low.
If you send a '1', and pin 2 is high, the 12 is returned.

I don't know how your switch is connected.

Do you want to send 12 if the switch is set high, at any moment ? If so, you have to rearrange your code.

The switch is connected like the one given in the arduino debounce tutorial.

The case is this. If I removed the code digitalRead(2)==HIGH

when I send a value of 1, it returns to me a value of 12

But I don't know what you want your program to do.....

I am programming my arduino to communicate with visual studio.

Since the arduino circuit that we have has a switch, once it is pressed a it will send a value to the visual studio then an image will be shown.

A part of the visual studio code

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        SerialPort1.Open()
        SerialPort1.Write("1")
        Basa = SerialPort1.ReadLine
        If (Basa = 12) Then
            PictureBox1.Visible = True
        End If
        SerialPort1.Close()

    End Sub

once the button in the visual studio is pressed, it will then send a value of 1 to the arduino.

From the arduino it needs to send a value of 12 back in order for the picture/image to visible

if we use this code of the arduino

void setup(){
  Serial.begin(9600);
  pinMode(5, INPUT);
  pinMode(13,OUTPUT);
}

void loop (){
while (Serial.available() == 0);
int val = Serial.read() - '0';

if(val==1)
{
  digitalWrite(13, HIGH);
  Serial.println(12);
}
Serial.flush();
}

it will throw a value of 12 back to visual studio allowing the image to be shown.

but if we use the code below

void setup(){
  Serial.begin(9600);
  pinMode(5, INPUT);
  pinMode(13,OUTPUT);
}

void loop (){
while (Serial.available() == 0);
int val = Serial.read() - '0';

if(val==1)
{
  digitalWrite(13, HIGH);
  call();
}
Serial.flush();
}

void call()
{
  if(digitalRead(5)==HIGH)
  {
  Serial.println(12);
  }
}

nothing happens even. the switch is not being read when it is pressed.

Are you trying to say that:
1; you are writing a C (or C++) program in Visual studio
2: That program will receive a value from the Arduino
3: The value will be sent via the serial port from the Arduino to the PC running the program.
4) Upon receiving the correct character/command/signal (whatever it is) the program will display an image.

Just curious.

Yes.

I've posted 2 arduino codes above only one is able to send a value back to the program allowing it to show the image.

Very simple serial test code that captures what is sent to the arduino from the serial monitor, and sends it back to the serial monitor.

// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial test 0021"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(2);  //delay to allow byte to arrive in input buffer
    char c = Serial.read();
    readString += c;
  }

  if (readString.length() >0) {
    Serial.println(readString);

    readString="";
  } 
}

@zoomkat

tried your code and yes the arduino can receive data from the serial monitor.


still figuring out the problem :expressionless: any help guys?

btw I'm using Arduino mega

Sorry, I can't see the problem.
Let's look at this in an other way.

How is your switch connected ? can you upload a schematic or photo.
Load the example "DigitalReadSerial" from the "01. Basics".
Set the variable pushButton to the pin of your switch.
Is that working ?

The switch is connected like this one

This one works when we load the basic program for debounce example, the button one and the digitalread serial.

Even I can’t find the problem itself :(.

Still trying to figure out what you are trying to do. I assume you want to push a button on the arduino, causing the arduino LED 13 to light and stay lit when the button is released. Also a single message is sent to the pc via the serial port, something like “LED lit”. Then when the button is released, LED 13 goes out and a single message like “LED off” is sent to the pc via the serial port. To only have a single message sent to the pc instead of a continous spew, you need to include toggle code to prevent the spew during looping. Below is button code for a servos but would be similar for LED 13. It shows the simple button code that does not require a resistor. The below code toggles, while the bottom requires the button to be held down. You can modify and add the serial messages to be sent for testing.

Button toggle

//zoomkat servo button toggle test 4-28-2012

#include <Servo.h>
int button = 5; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
boolean toggle = true;

void setup()
{
  pinMode(button, INPUT); //arduino monitor pin state
  servo.attach(7); //pin for servo control signal
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      servo.write(160);
      toggle = !toggle;
    }
    else
    {
      servo.write(20);
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

Button held down

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
  }
}

nothing happens even. the switch is not being read when it is pressed.

When are you pressing your button? The code you have right here tells your Arduino to do absolutely nothing until there is something in the serial buffer:

while (Serial.available() == 0);

When you do get something, you run through the rest of your loop in a split second, and return to doing absolutely nothing until you get something else in the serial buffer. Unless your button is pressed down during that split second after the serial data is received, you shouldn't be expecting anything. You should be using an if statement instead of a while statement so that you are not blocking your code.

zoomkat:

  while (Serial.available()) {

delay(2);  //delay to allow byte to arrive in input buffer
    char c = Serial.read();
    readString += c;
  }

If Serial.available() is non-zero there is data in the input buffer. You don't need the delay.

If that is the extent of the code and nothing is to be done beyod that code snipit,
Serial.available() is not needeed, because like who cares? 8)

Arrch:

nothing happens even. the switch is not being read when it is pressed.

When are you pressing your button? The code you have right here tells your Arduino to do absolutely nothing until there is something in the serial buffer:

Yep, nothing happens when I press the switch/button although if I test my button in another code (a simple one) it works.

@zoomkat
I'll test the code that you've posted thanks


erich1692:
Yep, nothing happens when I press the switch/button although if I test my button in another code (a simple one) it works.

Didn't exactly answer my question or address the issue that I brought up, but OK....

Sorry but I not pretty sure how to answer your question :\ . @Arrch

good news I've managed to fix the problem with the visual studio and arduino mega communication. Turns out I forgot to set something at the serial port tool in the vstudio program.

thanks for the help guys. I might post the code here soon though~