Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Help with pic18f452

Status
Not open for further replies.

Hubert J. F.

New Member
Hi,

I'm a complete newbie so I would really appreciate if someone could help me.

I need to move a servo motor from o to 180 degrees and turn on and off a led depending on the amount of light (ldr resistor).

I've made the program for arduino, but I need to program it in C18 (MPLAB).

Arduino program:
________________________________________
const int sensor=0;
const int led=13;
int level,high=0,low=1023,angle=0;
#include <Servo.h>
Servo myservo;

void setup(){
pinMode(led,OUTPUT);
Serial.begin(9600);
myservo.attach(9);}

void loop(){
level=analogRead(sensor);
manualTune();
analogWrite(led,level);

int valueLDR;
valueLDR=analogRead(A0);
Serial.println(valueLDR);
if(valueLDR<500){
myservo.write(0);
delay(10);}
else{
myservo.write(180);}}

void manualTune(){
level=map(level,0,1023,0,255);
level=constrain(level,0,255);}

void autoTune(){
if (level<low){
low=level;}
else{
high=level;}
level=map(level,low+30,high-30,0,255);
level=constrain(level,0,255);}
________________________________________

(PS: I apologize for my bad english, it isn't my first language and i'm still learning.)
 
Last edited:
The big issue here is... Arduino library code has all been done to death... Servo's, Serial, Mapping and the serial monitor are all C++ libraries..

To convert this simple code will take quite a bit of time... It would probably take me a couple of hours, But I don't have that sort of time to throw at it...

An easy fix would be BASIC where all these functions are available.... Take Oshonsoft basic... You could nearly throw that code in ( a few syntax re-coding ) but it would take a couple of minutes...
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top