Hello,
I've been trying to communicate between two programs on my computer and my arduino Uno but have had no luck so far. I'm pretty new to coding, but have about two years under my belt. I thought this would be enough for this project, but I was surprisingly wrong. I am trying to get my two programs to work one after the other and repeat. Like, the arduino code will iterate, then the console application will iterate. None of the example code I have tried has compiled, let alone work. I am using Visual Studio C++ on ym computer and a standard arduino sketch on the uno.
My console application code is:
// ConsoleApplication5.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <Windows.h>
#include <iostream>
using namespace std;
const int PIXEL_X = 1153;
const int PIXEL_Y = 636;
const int RED = 0;
const int BLUE = 50;
const int GREEN = 200;
const int TOLERANCE = 50;
int main(void) {
COLORREF color;
HDC hDC;
do {// Get the device context for the screen
Sleep(50);
hDC = GetDC(NULL);
// Retrieve the color at that position
color = GetPixel(hDC, PIXEL_X, PIXEL_Y);
// Release the device context again
ReleaseDC(GetDesktopWindow(), hDC);
printf("%i %i %i", GetRValue(color), GetGValue(color), GetBValue(color));
int flag = 0;
if (fabs(GetRValue(color) - RED) > TOLERANCE) {
flag += 1;
}
if (fabs(GetGValue(color) - GREEN) > TOLERANCE) {
flag += 1;
}
if (fabs(GetBValue(color) - BLUE) > TOLERANCE) {
flag += 1;
}
if (flag >= 2) {
return 0;
}
//TODO 1: Send Serial data to continue and wait for return statement
} while (true);
return 0;
}
This is checks a pixel on my desktop and continues if two flags don't stop it. This is then supposed to trigger my arduino code to iterate which is as follows:
Then once the arduino code iterates, then this is supposed to trigger the console application. This is supposed to continue until those two flags are tripped and the program ends.
Both of these programs work on their own, but I have nothing to get them to work in tandem. The trigger points are both of the TODOs in the code. I don't know what classes are and I haven't got any serial communication to work within my console code. Any information will help, thanks.
tyler303047:
This is then supposed to trigger my arduino code to iterate which is as follows:
I know nothing about Visual Studio (don't use Windows) but I don't see any code in your Arduino program to receive data.
Have a look at Serial Input Basics and design your PC program to work with the 2nd or (preferably) the 3rd example.
This Python - Arduino demo may help to illustrate the relationship between a PC program and an Arduino. The techniques should apply in any PC language.
Ensure that your PC program opens the serial port and keeps it open until it is completely finished with the Arduino.
Then once the arduino code iterates, then this is supposed to trigger the console application.
Sending serial data to the PC does NOT start an application on the PC. So, you must mean something else by "trigger". What you mean is NOT clear.
This is supposed to continue until those two flags are tripped and the program ends.
The two flags being the red, green, and blue values of a specific pixel? Having difficulties counting?
How is the Arduino waving a servo or two around uselessly supposed to have any impact on the color of a pixel on a monitor attached to a different computer?
Robin2:
I know nothing about Visual Studio (don't use Windows) but I don't see any code in your Arduino program to receive data.
Have a look at Serial Input Basics and design your PC program to work with the 2nd or (preferably) the 3rd example.
This Python - Arduino demo may help to illustrate the relationship between a PC program and an Arduino. The techniques should apply in any PC language.
Ensure that your PC program opens the serial port and keeps it open until it is completely finished with the Arduino.
...R
Thank you for the response.
I updated my code, but my problem more arises from the visual studio side of things.
Anything I try to use to open the serial port and then send either a 0 or a 1 doesn't compile. Apologies for not making that clear before.
but I got that the identifier was not declared, but I can't find how to declare it, and I have only ever seen this within classes, but I don't know what classes are.
Sorry for not making my question clear. I mean to send serial data( a 1 or a 0) from the visual studio program, for the arduino to read that data, and for the arduino to iterate if that data is a logic 0. Then I want the arduino to write serial data to the visual studio program(another 1 or a 0), then for the visual studio program to iterate if the data is a 0. Continuing on until the visual studio program sends a 1.
The flags are just shorthand logic for me to show that what I am looking for has happened.
To what I am trying to do. I am trying to make a shiny hunter for pokemon. The servos are already set up to press their buttons and the pixel is shown through a webcam onto my desktop, which is where getPixel comes in.
Please post the latest versions of your programs so we are all looking at the same thing. And please don't make changes to earlier posts other than to correct a typo.
// ConsoleApplication5.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <Windows.h>
#include <iostream>
using namespace std;
const int PIXEL_X = 1153;
const int PIXEL_Y = 636;
const int RED = 0;
const int BLUE = 50;
const int GREEN = 200;
const int TOLERANCE = 50;
int main(void) {
COLORREF color;
HDC hDC;
do {// Get the device context for the screen
Sleep(50);
hDC = GetDC(NULL);
// Retrieve the color at that position
color = GetPixel(hDC, PIXEL_X, PIXEL_Y);
// Release the device context again
ReleaseDC(GetDesktopWindow(), hDC);
printf("%i %i %i", GetRValue(color), GetGValue(color), GetBValue(color));
int flag = 0;
if (fabs(GetRValue(color) - RED) > TOLERANCE) {
flag += 1;
}
if (fabs(GetGValue(color) - GREEN) > TOLERANCE) {
flag += 1;
}
if (fabs(GetBValue(color) - BLUE) > TOLERANCE) {
flag += 1;
}
if (flag >= 2) {
return 0;
}
serialPort1->PortName = "COM3"; // Replace with your COM port!
serialPort1->Open();
serialPort1->Write("7"); // In the future, you'll expand on this
// to write your custom data to the board
serialPort1->Close();
} while (true);
return 0;
}
I think I can get the arduino to read the code, but I am still lost on the visual studio side.
results in a total delay of 16 x (1000 + 250 + 1500) millisecs. And the Arduino can do nothing during those delay()s. Have a look at how millis() is used to manage timing without blocking in several things at a time
Have you carefully studied the code in the links I gave you in Reply #1?
I also told you that this won't work
serialPort1->Open();
serialPort1->Write("7"); // In the future,
serialPort1->Close();
results in a total delay of 16 x (1000 + 250 + 1500) millisecs. And the Arduino can do nothing during those delay()s. Have a look at how millis() is used to manage timing without blocking in [several things at a time](http://forum.arduino.cc/index.php?topic=223286.0)
Have you carefully studied the code in the links I gave you in Reply #1?
I also told you that this won't work
serialPort1->Open();
serialPort1->Write("7"); // In the future,
serialPort1->Close();
...R
The delay is not my issue. Those delays are when the servos are supposed to activate. My issue is that I can't send data in visual studio. No example code I've seen works. I see many people talking about "starting" with one character through the serial port, which could work if I had some code to work with. I don't know what functions I should be using or how. That code I added was just something that I found that might work because PAul asked me to post SOMETHING. "I also told you that this won't work." I have no idea why that doesn't work or what I should change. My program isn't going to activate other programs. I just want the two programs to act as button presses for the other program.
When you open the serial port, the Arduino resets. Sending it data before it completes resetting is pointless. Think of opening the port, sending data, and closing the port as waking someone up, whispering a number, and then having them go back to sleep. Can you imagine asking them the number you whispered before they woke up, and having them be able to tell you the number?
The VB app should have a Connect button. In the click() callback, open the port, enable the Send and Disconnect objects, and disable the Connect object.
The VB app should have a Disconnect button. In the click() callback, close the port, enable the Connect object and disable the Disconnect and Send objects.
The VB app should have a text field and a Send button. In the click() callback, get the data from the text field, and send it.