Skip to content

Instantly share code, notes, and snippets.

@kristjanvariksoo
Created November 27, 2016 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kristjanvariksoo/50eaa56871707559af8bd34c13f5bd60 to your computer and use it in GitHub Desktop.
Save kristjanvariksoo/50eaa56871707559af8bd34c13f5bd60 to your computer and use it in GitHub Desktop.
#include <PID_v1.h>
#include <Servo.h>
#include <NewPing.h>
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);
Servo myservo;
int ECHO_PIN = 10;
int TRIGGER_PIN = 16;
int MAX_DISTANCE = 450;
int OUT = 2;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup () {
Serial.begin(960);
pinMode(14, OUTPUT);
digitalWrite(14, HIGH);
myservo.attach(OUT);
//initialize the variables we're linked to
Input = sonar.ping_cm();
Setpoint = 50;
//turn the PID on
myPID.SetMode(AUTOMATIC);
myPID.SetOutputLimits(1150, 2000);
}
void loop () {
Input = sonar.ping_cm();
myPID.Compute();
myservo.writeMicroseconds(Output);
Serial.print("Ping: ");
Serial.print(Input);
Serial.print("cm - out: ");
Serial.println(Output);
}
int INPIN = A3;
int LOWPIN = 8;
int OUTPIN = 9;
int LOWPIN2 = 6;
int OUTPIN2 = 7;
int TRESH = 1500;
void setup() {
// put your setup code here, to run once:
pinMode(INPIN, INPUT);
pinMode(OUTPIN, OUTPUT);
pinMode(LOWPIN, OUTPUT);
Serial.begin(9600);
Serial.println("Tere...?");
digitalWrite(LOWPIN, LOW);
digitalWrite(LOWPIN2, LOW);
}
unsigned long pulse;
boolean isHigh;
void loop() {
// put your main code here, to run repeatedly:
pulse = pulseIn(INPIN, HIGH);
Serial.print("Got pulse: ");
Serial.println(pulse);
isHigh = (pulse >= TRESH);
if (pulse >= TRESH) {
Serial.print(pulse);
Serial.print(" is GREATER than treshold of ");
Serial.print(TRESH);
Serial.println(".");
}
else {
Serial.print(pulse);
Serial.print(" is LOWER than treshold of ");
Serial.print(TRESH);
Serial.println(".");
}
digitalWrite(OUTPIN, isHigh);
digitalWrite(OUTPIN2, isHigh);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment