Click here to Skip to main content
15,908,115 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How to disable a property page in a property sheet in mfc.? Pin
David Crow1-Jun-13 7:20
David Crow1-Jun-13 7:20 
QuestionC++ and Arduino interfacing Pin
nonness31-May-13 23:02
nonness31-May-13 23:02 
QuestionRe: C++ and Arduino interfacing Pin
David Crow1-Jun-13 7:19
David Crow1-Jun-13 7:19 
AnswerRe: C++ and Arduino interfacing Pin
nonness3-Jun-13 10:03
nonness3-Jun-13 10:03 
QuestionRe: C++ and Arduino interfacing Pin
CPallini2-Jun-13 11:00
mveCPallini2-Jun-13 11:00 
AnswerRe: C++ and Arduino interfacing Pin
nonness3-Jun-13 10:04
nonness3-Jun-13 10:04 
QuestionRe: C++ and Arduino interfacing Pin
CPallini3-Jun-13 10:31
mveCPallini3-Jun-13 10:31 
AnswerRe: C++ and Arduino interfacing Pin
nonness4-Jun-13 0:18
nonness4-Jun-13 0:18 
I have a code which turns an arduino's pin on and off. This the code:
C++ script:
C++
#include "stdafx.h"

using namespace System;
using namespace System::IO::Ports;

int main(array<System::String ^> ^args)
{
	
	String^ answer;
	String^ portName;
	int baudRate=9600;
	Console::WriteLine("Type in a port name and hit ENTER");
	portName=Console::ReadLine();
	// arduino settings
	SerialPort^ arduino;
	arduino = gcnew SerialPort(portName, baudRate);
	// open port
	try
	{
		arduino->Open();

		do
		{
			// ask on or off
			Console::WriteLine("Type \"on\" to turn the light on or \"off\" to turn it off");
			// get answer
			answer=Console::ReadLine();
			//check that user typed one of the options
			if(String::Compare(answer,"on")==0)
				arduino->WriteLine("1"); // send 1 to arduino
			else if(String::Compare(answer,"off")==0)
				arduino->WriteLine("0"); // send 0 to arduino
			else
				Console::WriteLine(answer+" was not an option");
			// ask user if he wants to continue
			Console::WriteLine("Try again? yes/no");
			// get answer
			answer=Console::ReadLine();
			// clear the screen
			Console::Clear();
		}while(String::Compare(answer,"yes")==0);
		// close port to arduino
		arduino->Close();
	}
	catch (IO::IOException^ e  ) 
	{ 
		Console::WriteLine(e->GetType()->Name+": Port is not ready");
	}
	catch (ArgumentException^ e)
	{
		Console::WriteLine(e->GetType()->Name+": incorrect port name syntax, must start with COM/com");
	}
	// end program
	Console::Write("Press enter to close the program");
	Console::Read();
    return 0;
}

Arduino sketch:
C++
int ledPin = 13;
int state=0;
void setup() {
    pinMode(ledPin, OUTPUT); // pin will be used to for output
    Serial.begin(9600); // same as in your c++ script
}

void loop() {
  if (Serial.available() > 0)
  {
    state = Serial.read(); // used to read incoming data
    
    switch(state)// see what was sent to the board
    {
      case '1': // if the the one was sent
        digitalWrite(ledPin,HIGH);
      break;
      case '0': // if 0 was sent
        digitalWrite(ledPin,LOW);
      break;
      default:
      break; 
    }
  }
}

Now when i modify the code : when i send a word to the arduino it must return the ascii code of the first letter and the words are : Left Right Forward Back. So, i wrote:
C++
#include "stdafx.h"

using namespace System;
using namespace System::IO::Ports;
int main()

{
	String^ answer;
	String^ portName;
	int baudRate=9600;
	Console::WriteLine("Type in a port name and hit ENTER");
	portName=Console::ReadLine();
	// arduino settings
	SerialPort^ arduino;
	arduino = gcnew SerialPort(portName, baudRate);
try
	{
		arduino->Open();

		do
		{
			// ask on or off
			Console::WriteLine("Type \"on\" to turn the light on or \"off\" to turn it off");
			// get answer
			answer=Console::ReadLine();
			//check that user typed one of the options

			if (((350<=x)&&(x<=550))&&((200<=y)&&(y<=300)) )
			{
			cout << "Left" ;
			arduino->WriteLine("76");

			cout << "\n" ;
			}
			if (((100<=x)&&(x<=250))&&((200<=y)&&(y<=300))){
				cout << "Right" ;
				arduino->WriteLine("82");
			cout << "\n" ;
			}if (((300<=x)&&(x<=350))&&((300<=y)&&(y<=400))){
				cout << "Back";
			arduino->WriteLine("66");
			cout << "\n" ;
			}
			if (((300<=x)&&(x<=350))&&((80<=y)&&(y<=200))){
				cout << "Froward" ;
			arduino->WriteLine("70");
			cout << "\n" ;
			}
		Console::WriteLine("Try again? yes/no");
			// get answer
			answer=Console::ReadLine();
			// clear the screen
			Console::Clear();
			
			}while(String::Compare(answer,"yes")==0);
		// close port to arduino
		arduino->Close();
	}
	catch (IO::IOException^ e  ) 
	{ 
		Console::WriteLine(e->GetType()->Name+": Port is not ready");
	}
	catch (ArgumentException^ e)
	{
		Console::WriteLine(e->GetType()->Name+": incorrect port name syntax, must start with COM/com");
	}
	// end program
	Console::Write("Press enter to close the program");
	Console::Read();
  
}

SuggestionRe: C++ and Arduino interfacing Pin
David Crow4-Jun-13 3:56
David Crow4-Jun-13 3:56 
GeneralRe: C++ and Arduino interfacing Pin
nonness4-Jun-13 4:10
nonness4-Jun-13 4:10 
QuestionD flipflop implementation in C++ Pin
Manoj739031-May-13 19:51
Manoj739031-May-13 19:51 
AnswerRe: D flipflop implementation in C++ Pin
Richard MacCutchan31-May-13 21:36
mveRichard MacCutchan31-May-13 21:36 
GeneralRe: D flipflop implementation in C++ Pin
Manoj73902-Jun-13 19:10
Manoj73902-Jun-13 19:10 
GeneralRe: D flipflop implementation in C++ Pin
Richard MacCutchan2-Jun-13 20:45
mveRichard MacCutchan2-Jun-13 20:45 
QuestionRe: D flipflop implementation in C++ Pin
CPallini3-Jun-13 10:34
mveCPallini3-Jun-13 10:34 
AnswerRe: D flipflop implementation in C++ Pin
Manoj73903-Jun-13 21:22
Manoj73903-Jun-13 21:22 
QuestionHow to know when a video file has finished playing....MFC? Pin
mbatra3131-May-13 4:21
mbatra3131-May-13 4:21 
QuestionRe: How to know when a video file has finished playing....MFC? Pin
David Crow31-May-13 4:28
David Crow31-May-13 4:28 
AnswerRe: How to know when a video file has finished playing....MFC? Pin
mbatra3131-May-13 22:11
mbatra3131-May-13 22:11 
AnswerRe: How to know when a video file has finished playing....MFC? Pin
SoMad31-May-13 22:44
professionalSoMad31-May-13 22:44 
QuestionHow to display a progress bar while video is playing...MFC.? Pin
mbatra3130-May-13 19:51
mbatra3130-May-13 19:51 
AnswerRe: How to display a progress bar while video is playing...MFC.? Pin
David Crow31-May-13 4:02
David Crow31-May-13 4:02 
GeneralRe: How to display a progress bar while video is playing...MFC.? Pin
mbatra3131-May-13 4:04
mbatra3131-May-13 4:04 
Questionbinding client and server Pin
noislude30-May-13 10:13
noislude30-May-13 10:13 
AnswerRe: binding client and server Pin
Richard MacCutchan30-May-13 21:11
mveRichard MacCutchan30-May-13 21:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.