element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Uploading Raw C Onto Arduino
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 1 reply
  • Subscribers 390 subscribers
  • Views 257 views
  • Users 0 members are here
  • help
  • arduino
Related

Uploading Raw C Onto Arduino

swankybarbecue4
swankybarbecue4 over 7 years ago

My team member left and I have to pick up where he left off. He finished the code for our project (I pasted the code below as well as attached a file) in C and now I have to upload it to and Arduino Mega 2560. I only understand basic C and I am not familiar with the syntax requirements for the Arduino IDE. I assume this section at the top of the code sting call upon libraries (which I am currently learning about):

 

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\system.h"

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\led.h"

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\resistiveSensors.h"

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\powerDriver.h"

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\co2.h"

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\button.h"

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\usb.h"

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\sdcard.h"

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\dataLog.h"

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\camera.h"

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\dateTime.h"

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\expansion.h"

 

My question is (for clarification) how do I upload this code onto an Arduino Mega 2560? Is it as simple as downloading these listed libraries, changing the file address for each library in the code, and then copy and pasting everything into the Arduino IDE?

 

Thanks!

 

---CODE---

 

/*

* File:   controlProgram.c

* Author: David

*

* Created on November 9, 2016, 2:37 PM

*/

 

 

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\system.h"

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\led.h"

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\resistiveSensors.h"

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\powerDriver.h"

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\co2.h"

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\button.h"

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\usb.h"

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\sdcard.h"

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\dataLog.h"

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\camera.h"

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\dateTime.h"

#include "C:\Users\Hi2yo\MPLABXProjects\CONTROL.X\core\expansion.h"

 

 

 

 

 

 

 

 

/* This is written by David

*

* +++overall psueducode+++

*

* initialize system

*

* set scanner resolutions

*

* return motor to position 0

*

* scan sample 0

*

* record data 0

*

* write to SD card 0

*

* move motor to position 1

*

* scan sample 1

*

* record data 1

*

* write to SD card 0

*

* move motor to position 2

*

* scan sample 2

*

* record data 2

*

* write to SD card 0

*

* move motor to position 3

*

* scan sample 3

*

* record data 3

*

* write to SD card 0

*

* repeat

*

* +++END+++

*

* +++motor control pseudocode+++

 

* Position 1:

*

* rotate motor until resistiveSensor Q1 reads 5V

*

* stop motor

*

* Position 2:

*

* rotate motor until resistiveSensor Q1 reads 3.33V

*

* Position 3:

*

* rotate motor until resitiveSensor Q1 reads 1.66V

*

* stop motor

*

* Position 4:

*

* rotate motor until resisitveSensor Q1 reads 0V

*

* stop motor

*

* +++END+++

*

* +++scan pseudocode+++

*

* turn on rumble packs via LEDR control for 5s

*

* turn off rumble packs

*

* turn on spec laser via LEDB controller

*

* turn on scanner clock clock

*

* turn on RST and SHT

*

* turn off RST after 1 clock cycle

*

* turn off SHT after 6 clock cycles

*

* wait 1 clock cycle

*

* turn on DATA

*

* turn off DATA after 1 clock cycle

*

* begin sampling VIDEO for 1s

*

* +++END+++

*

*

* +++revised scan pseudocode+++

*

Initialize expansion slots

 

 

Configure EC2 to analog input

 

 

Configure EC1, EC3, EC4, EC5, EC6, EC7, EC8 to digital outputs

 

 

Initialize clock signal via EC8

 

 

digital write a constant signal through EC3 and EC4 to M0 and M1 to determine the output resolution

 

 

Digital write HIGH through pins EC1 and EC6 to trigger Reset and Shutter functions

 

 

Digital write LOW to complete reset Cycle via EC6

 

 

Digital write LOW to complete Shutter cycle via EC1

 

 

Digital write HIGH to Data via EC7

 

 

Begin to take analog input from EC2

 

 

Digital write LOW to Data via EC7

 

 

Repeat as needed

 

 

* +++END+++

*

*

*/

 

 

int motorPosition;  //variable for potentiometer

int videoSignal;    //variable for data signal

 

 

void motorPosition0 ()   //declared function for motor position 0

    {

        motorPosition = (int)resistiveSensors.readQ1();        //reads Q1 value and records as a variable

               

        if (motorPosition < 1024);

        {

            powerDriverA.on();      // turns on motor

            while (motorPosition < 1024)   //continuously reads sensor values until desired value occurs

            {

                delay (1);

                motorPosition = (int)resistiveSensors.readQ1();

            }

            powerDriverA.off();   // turns off motor

        }

               

               

    }

 

 

void motorPosition1  ()  //Declared Function for motor position 1

    {

        motorPosition = (int)resistiveSensors.readQ1();     //reads Q1 value and records as a variable

               

        if (motorPosition < 683);

        {

            powerDriverB.on();      // turns on motor

            while (motorPosition < 683)   //continuously reads sensor values until desired value occurs

            {

                delay(1);

                motorPosition = (int)resistiveSensors.readQ1();

            }

            powerDriverB.off();   // turns off motor

        }

        if (motorPosition > 683);

        {

             powerDriverA.on();      // turns on motor

            while (motorPosition > 683)   //continuously reads sensor values until desired value occurs

            {

                delay(1);

                motorPosition = (int)resistiveSensors.readQ1();

            }

            powerDriverA.off();   // turns off motor   

        }

    }

 

 

void motorPosition3 ()       //Declared Function for motor position 3

    {

        motorPosition = (int)resistiveSensors.readQ1();       //reads Q1 value and records as a variable

               

        if (motorPosition < 0);

        {

            powerDriverB.on();      // turns on motor

            while (motorPosition < 0)   //continuously reads sensor values until desired value occurs

            {

                delay(1);

                motorPosition = (int)resistiveSensors.readQ1();

            }

            powerDriverB.off(); // turns off motor

        }

        if (motorPosition > 0);

        {

             powerDriverA.on();      // turns on motor

            while (motorPosition > 0)   //continuously reads sensor values until desired value occurs

            {

                delay(1);

                motorPosition = (int)resistiveSensors.readQ1();

            }

            powerDriverA.off();   // turns off motor   

        }

    }

   

void recordData ()

{

    dataLog.add ("time", (int)dateTime.get);

    dataLog.add ("Absorbtion", videoSignal);

}

 

 

void scan ()

    {

       

        ledR.dutycycle(50);         //engages rumble packs for 5 seconds

        delay (5000);

        ledR.dutycycle(0);          //shuts off rumble packs

       

        ledB.dutycycle(50);         //turns on laser

   

   

        expansion.digitalOut (6,1);    //set resolution

        expansion.digitalOut (5,0);

 

 

        expansion.digitalOut (8,0);   //begin scanner clock cycle

        delay (500);

        expansion.digitalOut (10,1);

        delay (500);

        expansion.digitalOut (10,0);

        delay (250);

        expansion.digitalOut (3,1);  //opens shutter

        expansion.digitalOut (8,1); //resets all pixels to 0

        delay (250);

        expansion.digitalOut (10,0);

        delay (250);

        expansion.digitalOut (8,0);  //ends reset cycle, pixels begin doing things

        delay (250);

        expansion.digitalOut (10,1);

        delay (500);

        expansion.digitalOut (10,0);

        delay (500);

        expansion.digitalOut (10,1);

        delay (500);

        expansion.digitalOut (10,0);

        delay (500);

        expansion.digitalOut (10,1);

        delay (500);

        expansion.digitalOut (10,0);

        delay (500);

        expansion.digitalOut (10,1);

        delay (500);

        expansion.digitalOut (10,0);

        delay (500);

        expansion.digitalOut (10,0);

        delay (250);

        expansion.digitalOut (3,0);  //closes shutter, pixels stop doing things

        delay (250);

        expansion.digitalOut (10,1);

        delay (250);

        expansion.digitalOut (10,0);

        delay (250);

        expansion.digitalOut (9,1);  //tells sensor to send data

        delay (250);

        expansion.digitalOut (10,1);  //sensor starts sending data

        delay (250);

        expansion.digitalOut (9,0);  //shuts off data signal, data keeps sending

        delay (250);

        expansion.digitalOut (10,0);

        delay (500);

        expansion.digitalOut (10,1);

        delay (100);

        videoSignal = (int)resistiveSensors.readQ2;  //reads video signal and records it as a variable

        delay (500);

        expansion.digitalOut (10,0);        //shuts off sensor and waits 1 second

        delay(1000);

       

        ledB.dutycycle(0);      //shuts off laser

  

    }

 

 

void motorPosition2 ()    //Declared function for motor position 2

    {

        motorPosition = (int)resistiveSensors.readQ1() ;      //reads Q1 value and records as a variable

               

        if (motorPosition < 342);

        {

            powerDriverB.on();      // turns on motor

            while (motorPosition < 342)   //continuously reads sensor values until desired value occurs

            {

                delay(1);

                motorPosition = (int)resistiveSensors.readQ1();

            }

            powerDriverB.off();   // turns off motor

        }

        if (motorPosition > 342);

        {

             powerDriverA.on();      // turns on motor

            while (motorPosition > 342)   //continuously reads sensor values until desired value occurs

            {

                delay(1);

                motorPosition = (int)resistiveSensors.readQ1();

            }

            powerDriverA.off();   // turns off motor   

        }

    }

   

   

int main(int argc, char** argv)

{

    system.init();        //setup code to be run once

    usb.init();

    sdcard.init();

    ledR.init();

    ledB.init();

    dataLog.init();

    resistiveSensors.init();

    expansion.init();

    powerDriverA.init();

    powerDriverB.init();

   

   

 

 

    while (1==1)    //actual program to run forever or until power off

    {

        motorPosition0();

        scan();

        recordData();

        motorPosition1();

        scan();

        recordData();

        motorPosition2();

        scan();

        recordData();

        motorPosition3();

        scan();

        recordData();

    }

}

Attachments:
copy main.c.zip
  • Sign in to reply
  • Cancel

Top Replies

  • shabaz
    shabaz over 7 years ago +2
    Hi Merrick, This code was written for (most likely) a PIC processor because there are references to ' MPLABX ' in the code, and if you wish to port it to an Arduino Mega, you'll have to go through each…
Parents
  • shabaz
    shabaz over 7 years ago

    Hi Merrick,

     

    This code was written for (most likely) a PIC processor because there are references to 'MPLABX' in the code, and if you wish to port it to an Arduino Mega, you'll have to go through each line and see what the Arduino libraries offer you, and change the code accordingly. It might not be a lot of changes but there is no shortcut, it is some work effort that someone will need to do (either you, or a new team member perhaps), if you wish to port this code across to an Arduino Mega. The Arduino uses C too, so in terms of structure you can likely keep that the same, and instead of a 'main' function, put it in a function called 'loop'. More info on that here:

    Introduction to Arduino

    You'll have to delete most of the #includes because they refer to MPLAB X supplied header files, and replace (where relevant) with the Arduino equivalents, which will mean googling for (say) 'arduino sdcard' etc.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • shabaz
    shabaz over 7 years ago

    Hi Merrick,

     

    This code was written for (most likely) a PIC processor because there are references to 'MPLABX' in the code, and if you wish to port it to an Arduino Mega, you'll have to go through each line and see what the Arduino libraries offer you, and change the code accordingly. It might not be a lot of changes but there is no shortcut, it is some work effort that someone will need to do (either you, or a new team member perhaps), if you wish to port this code across to an Arduino Mega. The Arduino uses C too, so in terms of structure you can likely keep that the same, and instead of a 'main' function, put it in a function called 'loop'. More info on that here:

    Introduction to Arduino

    You'll have to delete most of the #includes because they refer to MPLAB X supplied header files, and replace (where relevant) with the Arduino equivalents, which will mean googling for (say) 'arduino sdcard' etc.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube