Last active
July 29, 2016 14:17
-
-
Save GuitouFlc/a89f79f082f60b67486df5706f11ab22 to your computer and use it in GitHub Desktop.
add const correcX and correctY
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* sketch Arduino basé sur le travail de https://github.com/camerontech | |
Adapté et modifié par Guillaume FLOCH | |
site web : www.osdclic.fr | |
tuto : www.memorandum.ovh | |
membre de : | |
www.goldorhack.org | |
Affichage de l'angle sur lcd avec indicateur d'angle max et avertisseur d'approche d'angle max. | |
*/ | |
#include <SoftwareSerial.h> | |
#include <Wire.h> | |
#include <LiquidCrystal_I2C.h> | |
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); | |
SoftwareSerial Serial1(3,2); | |
const bool DEBUG = true; | |
const int ledPin = 13; | |
const int X_PIN = 0; | |
const int Y_PIN = 1; | |
const int Z_PIN = 2; | |
const int DEGREE = 223; | |
const int MIN = 285; | |
const int MAX = 515; | |
/* | |
MERCI PIOU PIOU pour cette idée. | |
maxang angle maxi souhaité en ° -> declenche l'allumage d'une led | |
maxangN angle négatif maxi souhaité en ° (devrait etre l'inverse de maxang) -> declenche l'allumage d'une led | |
preA zone de préavertissement en ° ->declenche le clignottement de la led x° avant d'atteindre nos angle max 0 pour desactiver | |
*/ | |
const int maxang = 50; | |
const int maxangN = -50; | |
const int preA = 10; | |
const int correctX = -3; | |
const int correctY = 0; | |
void setup() { | |
pinMode(ledPin, OUTPUT); | |
if (DEBUG) | |
Serial.begin(9600); | |
lcd.begin(16,2); | |
Serial1.begin(9600); | |
lcd.begin(16,2); | |
Serial1.write("BE SAFE"); | |
lcd.setCursor(1,0); | |
lcd.print("soyez prudent "); | |
lcd.setCursor(1,1); | |
lcd.print("sur les routes"); | |
delay(2000); | |
clearDisplay(); | |
/*<< >> correspond à l'angle gauche droitet et ^^ correspond à l'angle avant arriere */ | |
Serial1.write(" << >> ^^ "); | |
lcd.print(" << >> ^^ "); | |
} | |
void loop() { | |
int x = analogRead(X_PIN); | |
int y = analogRead(Y_PIN); | |
int z = analogRead(Z_PIN); | |
int xAng = map(x, MIN, MAX, -90, 90); | |
int yAng = map(y, MIN, MAX, -90, 90); | |
int zAng = map(z, MIN, MAX, -90, 90); | |
int pitch = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI)+ correctX; | |
int roll = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI)+ correctY; | |
if (pitch > 180) { | |
pitch = pitch - 360; | |
} | |
if (roll > 180) { | |
roll = roll - 360; | |
} | |
if ((pitch > maxang) || (pitch < maxangN)) { | |
digitalWrite(ledPin, LOW); | |
} | |
else if ((pitch > maxangN+preA) && (pitch < maxang-preA)){ | |
digitalWrite(ledPin, HIGH); | |
} | |
else { | |
digitalWrite(ledPin, HIGH); | |
delay(250); | |
digitalWrite(ledPin, LOW); | |
} | |
updateDisplay(pitch, roll); | |
delay(250); | |
} | |
void updateDisplay(int pitch, int roll) { | |
moveToSecondLine(); | |
String pitchString = String(pitch); | |
String rollString = String(roll); | |
String output = " "; | |
if (pitchString.length() == 1) { | |
output += " "; | |
} | |
output += pitchString; | |
Serial1.print(output); | |
Serial1.write((char)223); | |
lcd.print(output); | |
lcd.print((char)223); | |
int outputLength = output.length() + 1; | |
output = ""; | |
for (int i=outputLength; i<10; i++) { | |
output += " "; | |
} | |
if (rollString.length() == 1) { | |
output += " "; | |
} | |
output += rollString; | |
Serial1.print(output); | |
Serial1.write(DEGREE); | |
lcd.print(output); | |
lcd.print((char)223); | |
outputLength += output.length() + 1; | |
for (int i=outputLength; i<16; i++) { | |
Serial1.print(" "); | |
lcd.print(" "); | |
} | |
if (DEBUG){ | |
Serial.print("pitch: "); | |
Serial.print(pitchString); | |
Serial.print(" roll: "); | |
Serial.print(rollString); | |
Serial.println(""); | |
lcd.print("pitch: "); | |
lcd.print(pitchString); | |
lcd.print("roll: "); | |
lcd.print(rollString); | |
lcd.print(""); | |
} | |
} | |
void clearDisplay() { | |
moveToFirstLine(); | |
Serial1.write(" "); | |
Serial1.write(" "); | |
moveToFirstLine(); | |
lcd.print(" "); | |
lcd.print(" "); | |
} | |
void moveToFirstLine() { | |
Serial1.write(254); | |
Serial1.write(128); | |
lcd.setCursor(7,1); | |
} | |
void moveToSecondLine() { | |
Serial1.write(254); | |
Serial1.write(192); | |
lcd.setCursor(1,1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment