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 I need help in converting Arduino code to C language code
  • 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 38 replies
  • Subscribers 400 subscribers
  • Views 6593 views
  • Users 0 members are here
Related

I need help in converting Arduino code to C language code

Former Member
Former Member over 9 years ago

 

#include

 


Servo myservo; //creates a servo object
//a maximum of eight servo objects can be created

 

int pos = 0; //variable to store servo position

 

 

 

int calibrationTime = 30;

 

//the time when the sensor outputs a low impulse
long unsigned int lowIn;

 

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;

 

boolean lockLow = true;
boolean takeLowTime;

 

int pirPin = 12; //digital pin connected to the PIR's output
int pirPos = 13; //connects to the PIR's 5V pin

 

void setup(){
myservo.attach(4); //attaches servo to pin 4
Serial.begin(9600); //begins serial communication
pinMode(pirPin, INPUT);
pinMode(pirPos, OUTPUT);
digitalWrite(pirPos, HIGH);

 

//give the sensor time to calibrate
Serial.println("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(calibrationTime - i);
Serial.print("-");
delay(1000);
}
Serial.println();
Serial.println("done");

 


while (digitalRead(pirPin) == HIGH) {
delay(500);
Serial.print(".");
}
Serial.print("SENSOR ACTIVE");
}

 

void loop(){

 

if(digitalRead(pirPin) == HIGH){ //if the PIR output is HIGH, turn servo

 

/*turns servo from 0 to 180 degrees and back
it does this by increasing the variable "pos" by 1 every 5 milliseconds until it hits 180
and setting the servo's position in degrees to "pos" every 5 milliseconds
it then does it in reverse to have it go back
**/

 

for(pos = 0; pos < 180; pos += 1) //goes from 0 to 180 degrees
{ //in steps of one degree
myservo.write(pos); //tells servo to go to position in variable "pos"
delay(5); //waits for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) //goes from 180 to 0 degrees
{
myservo.write(pos); //to make the servo go faster, decrease the time in delays for
delay(5); //to make it go slower, increase the number.
}

 

if(lockLow){

 

lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}

 

if(digitalRead(pirPin) == LOW){

 

if(takeLowTime){
lowIn = millis(); //save the time of the transition from HIGH to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}

 

//if the sensor is low for more than the given pause,
//we can assume the motion has stopped

 

if(!lockLow && millis() - lowIn > pause){

 

lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
}

  • Sign in to reply
  • Cancel

Top Replies

  • COMPACT
    COMPACT over 9 years ago in reply to Former Member +3
    For which particular aspects of C++? C++ is really c with objects and some changes to syntax and structure to support objects (e.g. polymorphism). C++ can make the compiled object code bloated, fat and…
  • mconners
    mconners over 9 years ago in reply to Former Member +3
    Well, in that case the flavor of C used for the Arduino already meets your needs. C++ is a super set of the C language, and the Arduino compiler already supports this. A major difference is when using…
  • mconners
    mconners over 9 years ago +2
    Hi shajeeh, Arduino code is C language code. It just has a simplified API to allow you to access the IO pins available on the AVR chip. What platform are you wanting to convert this to? We would need that…
Parents
  • mconners
    mconners over 9 years ago

    Hi shajeeh,

     

    Arduino code is C language code. It just has a simplified API to allow you to access the IO pins available on the AVR chip. What platform are you wanting to convert this to?

     

    We would need that info prior to making any recommendations.

     

    Mike

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 9 years ago in reply to mconners

    Hi michael,

    I would like to convert it to c++.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • jomoenginer
    jomoenginer over 7 years ago in reply to COMPACT

    Uh, not completely true.   Actually C++ code could make the end binary smaller and increase the performance of the code if one knows what they are doing.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • jomoenginer
    jomoenginer over 7 years ago in reply to Former Member

    Could you clarify more of what you are looking to do.  Are you looking to completely get away from all of the Arduino code or are you looking to code using the Arduino using C++?   If you are looking to not use any of the Arduino libraries then you will either have to create them yourself or use other libs.

     

    What IDE are you looking to use or are you looking to just use command line dev?

     

    Are you familiar with AVR Studio?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • COMPACT
    COMPACT over 7 years ago in reply to jomoenginer

    If that's your belief, please enlighten us with an example of a compiled C++ program being smaller and faster than an C program.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • jomoenginer
    jomoenginer over 7 years ago in reply to COMPACT

    There are plenty of examples on the web that have dispelled most of the myths of C++ in Embedded systems.

     

    The idea that C++ creates bloated code is an old school mentality that is just not valid any more.   The modern C++ compilers and tools are much more efficient then they were in the past.  C on the other hand offers the developer a double barrel shotgun with the choke wide opened where one could blow off both feet if not careful.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • COMPACT
    COMPACT over 7 years ago in reply to jomoenginer

    I quickly searched the web and found many incorrect examples in the web.

    One site even compared compiling a C program on a C compiler and C++ compiler.

    This is a pointless exercise because for all intents and purposes produce identical code.

     

    There was even one example where the example C code was doing twice as much as it should as compared with its C++ equivalent and thus was taking just under twice as long.

    Well, If you'd remove the redundant part of the C code, then the C code would be faster that the C++ equivalent.

     

    Many of these poor examples rely upon the standard functions of C.

    A rule of C is that when you don't find one that is optimal for your purpose you can write your own.

    Source code is provided for the generic functions can be used for your optimised version.

     

    As for a being a "Double Barrelled Shotgun" for an advanced programmer - No.

    C is a very low level language that just hovers above Assembly Language so of course one has to be careful.

    C was written to write itself. That's how low a level language it is.

     

    The advantage of C is that is portable between target platforms and as such does not necessarily produce optimal code (but pretty close!).

    These day's is not much of an issue because of the fast processors around. Code for ancient processors operating at 500kHz or so needed to be optimal to work as real time applications.

     

    To expedite writing assembly language programs over 30 years ago, my team developed and used an in-house C syntax pre-processor for Assembly Language programming.

    It was used to make it simple and easier to deploy standard programming constructs.

    It removed the necessity of the Assembly Language programmer to worry about such matters.

     

    One nice thing about C is that the generated assembly output is commented with the corresponding C code.

    From this one can determine whether the code was generated efficiently or not for the target host architecture.

     

    C++ was spawned from C and thus is C but with additional features.

    So's Objective-C but that is a different kettle of fish.

     

    As a programmer you have a responsibility to know what your code is actually doing.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • jomoenginer
    jomoenginer over 7 years ago in reply to COMPACT

    COMPACT  wrote:

     

     

    To expedite writing assembly language programs over 30 years ago, my team developed and used an in-house C syntax pre-processor for Assembly Language programming.

    It was used to make it simple and easier to deploy standard programming constructs.

    It removed the necessity of the Assembly Language programmer to worry about such matters.

     

    One nice thing about C is that the generated assembly output is commented with the corresponding C code.

    From this one can determine whether the code was generated efficiently or not for the target host architecture.

     

    C++ was spawned from C and thus is C but with additional features.

    So's Objective-C but that is a different kettle of fish.

     

    As a programmer you have a responsibility to know what your code is actually doing.

    Thank you for validating my previous statement which I stand by 100%.

     

    Also, C++ is not C with additional features nor is it C with classes.  This again is an misconception.   Although C++ is a decedent of C, that is classic C and C89, after C99 they are considered siblings.  You can run C in a C++ config, however there are restrictions in that there is some C code that is not valid in C++. I tend take the word of the creator of C++ as the voice of logic when it comes to C++. 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • COMPACT
    COMPACT over 7 years ago in reply to jomoenginer

    What I said is not a misconception.

     

    The preface of Bjarne Stroustrup's  "The C++ Programming Language" book says:

         " Except for minor details, C++ is a superset of the C programming language."

     

    Then it goes to say that:

         "A key concept in C++ is class."

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • jomoenginer
    jomoenginer over 7 years ago in reply to COMPACT

    That is the from the Preface of the 1st edition that was published in 1985.

     

    Hence: "Although C++ is a decedent of C, that is classic C and C89, after C99 they are considered siblings."

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • neuromodulator
    neuromodulator over 7 years ago in reply to jomoenginer

    I think that's a bit of nitpicking, even in the era of C89 you could write C89 code that is not C++. I still consider C a subset of C++, that as you mention, has gotten more incompatible lately with the introduction of C99.


    Now back to Arduino, Arduino afik is C++, with a few limitations.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • jomoenginer
    jomoenginer over 7 years ago in reply to neuromodulator

    neuromodulator  wrote:

     

    I think that's a bit of nitpicking, even in the era of C89 you could write C89 code that is not C++. I still consider C a subset of C++, that as you mention, has gotten more incompatible lately with the introduction of C99.


    Now back to Arduino, Arduino afik is C++, with a few limitations.

    Regarding C being a subset of C++, to me that really makes no sense, but if you think so then go with it.

     

    As far as Arduino is concerned, from the Arduino FAQ it states:

      "the Arduino language is merely a set of C/C++ functions that can be called from your code"

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • jomoenginer
    jomoenginer over 7 years ago in reply to neuromodulator

    neuromodulator  wrote:

     

    I think that's a bit of nitpicking, even in the era of C89 you could write C89 code that is not C++. I still consider C a subset of C++, that as you mention, has gotten more incompatible lately with the introduction of C99.


    Now back to Arduino, Arduino afik is C++, with a few limitations.

    Regarding C being a subset of C++, to me that really makes no sense, but if you think so then go with it.

     

    As far as Arduino is concerned, from the Arduino FAQ it states:

      "the Arduino language is merely a set of C/C++ functions that can be called from your code"

    • Cancel
    • Vote Up 0 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