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.
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.
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.
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="";
}
}
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 ?
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.
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
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~