Skip to content

Instantly share code, notes, and snippets.

@SysOverdrive
Created March 14, 2016 22:03
Show Gist options
  • Save SysOverdrive/d0594bd263cf1313f016 to your computer and use it in GitHub Desktop.
Save SysOverdrive/d0594bd263cf1313f016 to your computer and use it in GitHub Desktop.
/*
Controlling a servo position using a potentiometer (variable resistor)
by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
modified on 8 Nov 2013
by Scott Fitzgerald
http://arduino.cc/en/Tutorial/Knob
*/
// la control la pornire trebe duty cicle sub 6%
/***
*vom folosi o frecenta de 500 de Herz rescriind libraria cu un timer digital
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = A0; // analog pin used to connect the potentiometer
int val, val1; // variable to read the value from the analog pin
int duty = 50;
int dc = 0;
//ISR Part
volatile unsigned long timp2;
volatile float frecventa;
int interuptPin=3;// we set this for platform switching purpose
unsigned long timp1;
char buffer[32];
void setup()
{
timp1 = millis();
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(0);
Serial.begin(115200);
//analogWrite(9,0);
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
//ISR part
pinMode(interuptPin, INPUT);
attachInterrupt(digitalPinToInterrupt(interuptPin), ISRFunction, RISING);
timp2 = micros();
frecventa = 0;
delay(3000);
}
//Task 01
// configureaza un timer la frecventa de 50 de kHz si sa pun o intrerupere la ea
// comuta un pin sus jos si vezi daca am 50 de kHz pe osciloscop
/*
*/
void timerSoftware()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val1 = map(val, 0, 500, 35, 180); // scale it to use it with the servo (value between 0 and 180)
val = map(val, 0, 300, 0, 100);
duty = val;
myservo.write(val1);
}
//---ISR PART
//Functia din ISR
void ISRFunction()
{
frecventa = 1000000/(float)(micros() - timp2);
timp2 = micros();
//Serial.print(frecventa);
}
//Frecventiometer
void Frecventiometer()
{
unsigned int frec1 = (int)frecventa; // pentru functia snprint care NU accepta double
unsigned int frec2 = (int)(frecventa * 10) % 10;
snprintf(buffer, sizeof(buffer), "%d.%d", frec1, frec2); // metoda antica toString NU merge cu double
Serial.println(buffer);
}
//timer software
void loop()
{
Frecventiometer();
//folosim milis sa facem ceva la intervale de timp predefinite
//----------------------------------------------ASTA !! Impreuna cu funtia timer software
if ((millis() - timp1) > 500) {
timp1 = millis();
timerSoftware();
Frecventiometer();
}
//-----------------------------------------------
}
/*
// myservo.write(180);
//analogWrite(9,180);
//delay(100);
//delay(100);
// myservo.write(50);
//analogWrite(9,50);
//delay(100);
//analogWrite(9,50);
// myservo.write(0);
//delay(100);
OLD CODE
vechiul timer software
if (dc>100) {
dc = 0;
timerSoftware();
}
delayMicroseconds(150);
--sfarsit timer
vechiul pwm softare
if (dc>duty) digitalWrite(2, LOW); else digitalWrite(2, HIGH);
dc++;
--sfarsit
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment