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 6604 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
  • lmc98cdddt16c
    lmc98cdddt16c over 7 years ago

    chào cac chàng trai,có thể giúp tôi về vấn đề chuyển đổi code của adruino sang code cua ccs cho arm được không

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

    Hello guys, can you help me on converting adruino code to ccs code for arm

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • shabaz
    shabaz over 7 years ago in reply to lmc98cdddt16c

    Do you mean Arduino? Or something else?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Fred27
    Fred27 over 7 years ago in reply to lmc98cdddt16c

    If by CCS you mean TI's Code Composer Studio then you can just open up an Energia sketch and run it on some of their ARM processors. I'm not up on the details because I avoid Arduino. You will find 43oh.com is a good resource for Energia stuff.

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

    You can start here, although the post is a bit older since CCS is at version 8

    https://43oh.com/2014/07/import-energia-sketches-into-ccs6-for-line-by-line-debugging/

     

    If you are looking to drop the Arduino code completely then you'll have a bit of work ahead for you and would have to create your own code to stimulate the Arduino libraries. That is unless you can find one within the SimpleLink packages or other code.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • lmc98cdddt16c
    lmc98cdddt16c over 7 years ago in reply to shabaz

    hi, specifically i want to translate this code from adurino side to MDK 5 KelC

    Can you help me?

    thank you very much

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • lmc98cdddt16c
    lmc98cdddt16c over 7 years ago in reply to Fred27

    Thank you so much, but the idea is to transfer adrunio code to code code of MDK 5 KelC, can you help me?

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

    Thank you very much, but the idea is to transfer adrunio code to code code of MDK 5 KelC, can you help me?

    code Adrunio:

    1. // (c) Michael Schoeffler 2017, http://www.mschoeffler.de
    2. #include <HX711_ADC.h> // https://github.com/olkal/HX711_ADC
    3. #include <Wire.h>
    4. #include <LiquidCrystal_I2C.h> // LiquidCrystal_I2C library
    5. HX711_ADC LoadCell(4, 5); // parameters: dt pin, sck pin<span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;"></span>
    6. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // 0x27 is the i2c address of the LCM1602 IIC v1 module (might differ)
    7. voidsetup(){
    8.   LoadCell.begin(); // start connection to HX711
    9.   LoadCell.start(2000); // load cells gets 2000ms of time to stabilize
    10.   LoadCell.setCalFactor(999.0); // calibration factor for load cell => strongly dependent on your individual setup
    11.   lcd.begin(16, 2); // begins connection to the LCD module
    12.   lcd.backlight(); // turns on the backlight
    13. }
    14. voidloop(){
    15.   LoadCell.update(); // retrieves data from the load cell
    16. float i = LoadCell.getData(); // get output value
    17.   lcd.setCursor(0, 0); // set cursor to first row
    18.   lcd.print("Weight[g]:"); // print out to LCD
    19.   lcd.setCursor(0, 1); // set cursor to secon row
    20.   lcd.print(i); // print out the retrieved value to the second row
    21. }
    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • lmc98cdddt16c
    lmc98cdddt16c over 7 years ago in reply to lmc98cdddt16c

    code Adruino:

    1. // (c) Michael Schoeffler 2017, http://www.mschoeffler.de
    2. #include <HX711_ADC.h> // https://github.com/olkal/HX711_ADC
    3. #include <Wire.h>
    4. #include <LiquidCrystal_I2C.h> // LiquidCrystal_I2C library
    5. HX711_ADC LoadCell(4, 5); // parameters: dt pin, sck pin<span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;"></span>
    6. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // 0x27 is the i2c address of the LCM1602 IIC v1 module (might differ)
    7. voidsetup(){
    8.   LoadCell.begin(); // start connection to HX711
    9.   LoadCell.start(2000); // load cells gets 2000ms of time to stabilize
    10.   LoadCell.setCalFactor(999.0); // calibration factor for load cell => strongly dependent on your individual setup
    11.   lcd.begin(16, 2); // begins connection to the LCD module
    12.   lcd.backlight(); // turns on the backlight
    13. }
    14. voidloop(){
    15.   LoadCell.update(); // retrieves data from the load cell
    16. float i = LoadCell.getData(); // get output value
    17.   lcd.setCursor(0, 0); // set cursor to first row
    18.   lcd.print("Weight[g]:"); // print out to LCD
    19.   lcd.setCursor(0, 1); // set cursor to secon row
    20.   lcd.print(i); // print out the retrieved value to the second row
    21. }
    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • lmc98cdddt16c
    lmc98cdddt16c over 7 years ago in reply to lmc98cdddt16c

    code Adrunio :

    1. // (c) Michael Schoeffler 2017, http://www.mschoeffler.de
    2. #include <HX711_ADC.h> // https://github.com/olkal/HX711_ADC
    3. #include <Wire.h>
    4. #include <LiquidCrystal_I2C.h> // LiquidCrystal_I2C library
    5. HX711_ADC LoadCell(4, 5); // parameters: dt pin, sck pin<span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;"></span>
    6. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // 0x27 is the i2c address of the LCM1602 IIC v1 module (might differ)
    7. voidsetup(){
    8.   LoadCell.begin(); // start connection to HX711
    9.   LoadCell.start(2000); // load cells gets 2000ms of time to stabilize
    10.   LoadCell.setCalFactor(999.0); // calibration factor for load cell => strongly dependent on your individual setup
    11.   lcd.begin(16, 2); // begins connection to the LCD module
    12.   lcd.backlight(); // turns on the backlight
    13. }
    14. voidloop(){
    15.   LoadCell.update(); // retrieves data from the load cell
    16. float i = LoadCell.getData(); // get output value
    17.   lcd.setCursor(0, 0); // set cursor to first row
    18.   lcd.print("Weight[g]:"); // print out to LCD
    19.   lcd.setCursor(0, 1); // set cursor to secon row
    20.   lcd.print(i); // print out the retrieved value to the second row
    21. }
    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • lmc98cdddt16c
    lmc98cdddt16c over 7 years ago in reply to lmc98cdddt16c

    code Adrunio :

    1. // (c) Michael Schoeffler 2017, http://www.mschoeffler.de
    2. #include <HX711_ADC.h> // https://github.com/olkal/HX711_ADC
    3. #include <Wire.h>
    4. #include <LiquidCrystal_I2C.h> // LiquidCrystal_I2C library
    5. HX711_ADC LoadCell(4, 5); // parameters: dt pin, sck pin<span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;"></span>
    6. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // 0x27 is the i2c address of the LCM1602 IIC v1 module (might differ)
    7. voidsetup(){
    8.   LoadCell.begin(); // start connection to HX711
    9.   LoadCell.start(2000); // load cells gets 2000ms of time to stabilize
    10.   LoadCell.setCalFactor(999.0); // calibration factor for load cell => strongly dependent on your individual setup
    11.   lcd.begin(16, 2); // begins connection to the LCD module
    12.   lcd.backlight(); // turns on the backlight
    13. }
    14. voidloop(){
    15.   LoadCell.update(); // retrieves data from the load cell
    16. float i = LoadCell.getData(); // get output value
    17.   lcd.setCursor(0, 0); // set cursor to first row
    18.   lcd.print("Weight[g]:"); // print out to LCD
    19.   lcd.setCursor(0, 1); // set cursor to secon row
    20.   lcd.print(i); // print out the retrieved value to the second row
    21. }
    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
  • mconners
    mconners over 7 years ago in reply to lmc98cdddt16c

    Hi cong le,

     

    The issue you'll run into is that the HX711_ADC and LiquidCrystal_I2C libraries are probably not available for keil.

     

    You can get the source and port them your self, which often isn't too terribly hard, and the source is usually available.

     

    You would also need to create a main() function and call setup() and loop() (or move those into main)

     

    Take a look at mbed.com, they have arduino like libraries for a variety of arm processors, and they have the capability to export to a few different dev environments such as keil, gcc, etc...

     

    This won't be terribly difficult as a mid level c (or c++) programmer.

     

     

    Mike

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • lmc98cdddt16c
    lmc98cdddt16c over 7 years ago in reply to mconners

    thanhks you so much!

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
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