Sunday 4 September 2022

Arduino USB Communication - Processing Program

 https://www.instructables.com/Arduino-USB-comunication-Example-Program/


/*********************************************************************************

**********************************************************************************

**************                                            ************************

**************        ARDUINO USB CONTROL EXAMPLE         ************************

**************                                            ************************

**********************************************************************************

*********************************************************************** William_19


This program is intended to control Arduino using USB port.


Instructions to use this program.


0- Be nice to people and do not pollute the environment :)


1- It was made to comunicate via USB with the PC


2- The communication program is made using "Processing" - Processing is an program language

very similar to Arduino. It is very easy to use and it's also very easy to do it

communicate with Arduino. If you don't know or if you are not familiar with, it worth 

take a look at http://www.processing.org/

We are sure you will enjoy learning it and soon you will be able to do your own Processing program.


3- this is a general program, it's not the final program... Use it just as a reference for future programs.


4- the program uses the following input's and outpout's:


        Pins 13,12,11,10,9,8 as digital outputs


        Pin 0 as an Analog Input (check line 164)

        

        **We haven't made digital inputs or analog outputs, but you can easily implement that.

        **you'll notice that some parts of the program are "missing", do not forget that this is an example program.

        


********************************************************************************/






//Defining global variables**********************************************

int output1 = 13; //define output 13 in the board as "output1" in the program

int output2 = 12; //define output 12 in the board as "output2" in the program

int output3 = 11; //define output 11 in the board as "output3" in the program

int output4 = 10; //define output 10 in the board as "output4" in the program

int output5 = 9;  //define output 9 in the board as "output5" in the program

int output6 = 8;  //define output 8 in the board as "output6" in the program


byte status_output1 = LOW;  //status OUTPUT 13 in the board and "output1" in the program

byte status_output2 = LOW;  //status OUTPUT 12 in the board and "output2" in the program

byte status_output3 = LOW;  //status OUTPUT 11 in the board and "output3" in the program

byte status_output4 = LOW;  //status OUTPUT 10 in the board and "output4" in the program

byte status_output5 = LOW;   //status OUTPUT 9 in the board and "output5" in the program

byte status_output6 = LOW;   //status OUTPUT 8 in the board and "output6" in the program



int receivedUSB; //will be used to get the "Serial.read()" - read serial port;

//**********************************************************************





void setup() {    


  Serial.begin(9600);   //Inicialize the Serial communication - Arduino

  

  //Inicializating board OUTPUTS/INPUTS**********************************************

  /*OBS.: you do not need to inicializate analog output PINS*/  

  pinMode(output1, OUTPUT);

  pinMode(output2, OUTPUT);

  pinMode(output3, OUTPUT);

  pinMode(output4, OUTPUT);

  pinMode(output5, OUTPUT);

  pinMode(output6, OUTPUT);

  //*******************************************************************************

 

  establishContact();  // send a byte to establish contact until receiver responds

}




void loop() {

  

  if (Serial.available()){ // Check if Arduino has received something

    

      receivedUSB = Serial.read(); //read serial port

     


      //if Arduino receives 34 (") *****************************************************

      

      /**** OBS.: Arduino receives ASII data, so when we say it's receives 34 we could also say it's

      receiving ("). It's an easy way of making Arduino USB communication  ***/

      

      if (receivedUSB == 34) { //check received signal

      

        status_output1 = !status_output1; // change the STATUS

        digitalWrite(output1, status_output1); //Assign Pin from LOW to HIGH

       }//**********************************************************************

        

        

      //if Arduino receives 35  (#) *****************************************************

      if (receivedUSB == 35) { //check received signal

      

        status_output2 = !status_output2; // change the STATUS

        digitalWrite(output2, status_output2); //Assign Pin from LOW to HIGH

       }//********************************************************************** 

        

        

       //if Arduino receives 36 ($)  *****************************************************

      if (receivedUSB == 36) { //check received signal

      

        status_output3 = !status_output3; // change the STATUS

        digitalWrite(output3, status_output3); //Assign Pin from LOW to HIGH

       }//**********************************************************************

        

        

      //if Arduino receives 37 (%)  *****************************************************

      if (receivedUSB == 37) { //check received signal

      

        status_output4 = !status_output4; // change the STATUS

        digitalWrite(output4, status_output4); //Assign Pin from LOW to HIGH

       }//**********************************************************************  

       

       

       //if Arduino receives 38 (&)  *****************************************************

      if (receivedUSB == 38) { //check received signal

      

        status_output5 = !status_output5; // change the STATUS

        digitalWrite(output5, status_output5); //Assign Pin from LOW to HIGH

       }//**********************************************************************

        

        

      //if Arduino receives 39 (')  *****************************************************

      if (receivedUSB == 39) { //check received signal

      

        status_output6 = !status_output6; // change the STATUS

        digitalWrite(output6, status_output6); //Assign Pin from LOW to HIGH

      }//****************************************************************************** 

      

      

      //if Arduino receives 40 (()  *****************************************************

      if (receivedUSB == 40) { //check received signal

      

      }//****************************************************************************** 

      

      

      //if Arduino receives 41 ())  *****************************************************

      if (receivedUSB == 41) { //check received signal

    

      }//****************************************************************************** 

      

      

      //if Arduino receives 42 (*)  *****************************************************

      if (receivedUSB == 42) { //check received signal

      

      }//****************************************************************************** 

      

      

      //if Arduino receives 43 (+)  *****************************************************

      if (receivedUSB == 43) { //check received signal

      

      }//****************************************************************************** 

      

       

       int analogA0 = analogRead(A0);  //reads Analog pin 0

       delay (2); //Delay for stabilizing the signal

       

       //Send the USB signal to the computer  *****************************************************

       /*OBS.: --The computer must be prepared to received "int" data, separeted by commas.

               --All the Data Arduino is goind to send to the computer must be here.

               --The processing program is read to receive this data. */

       

        Serial.print(analogA0);         //data 0

        Serial.print(",");

        Serial.print(111);              //data 1

        Serial.print(",");

        Serial.print(222);              //data 2

        Serial.print(","); 

        Serial.print(111);              //data 3

        Serial.print(",");  

        Serial.print(111);              //data 4

        Serial.print(",");

        Serial.println(7); //the last data has to be "println"     //data 5

      }//**********************************************************************************

  

      

 }   

      

      

      

      

void establishContact() {  //This function is used to start the communication

  while (Serial.available() <= 0) {

    Serial.println("0,0,0,0,0,0");   // send an initial string (6 ZEROS)

    //IT HAS TO BE EXACLY THE SAME LENGHT OF THE SIGNAL YOU ARE SENDING TO THE PC

    //WE ARE SENDING (DATA 0,1,2,3,4,5) -- WITCH GIVE US 6 DATA

    delay(300);

  }

}      

      

      

      

      

      

     


No comments:

Post a Comment