Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
wiki:projets:titrimetre2:programmation [2016/05/02 12:02] BAPTISTE Benoit |
wiki:projets:titrimetre2:programmation [2016/09/11 11:15] (Version actuelle) |
||
---|---|---|---|
Ligne 3: | Ligne 3: | ||
=====Partie programmation===== | =====Partie programmation===== | ||
- | #include < | ||
- | LiquidCrystal lcd(42, 41, 35, 34, 33, 32); //6 pin | ||
- | #include < | + | Voici le programme finale : |
- | const byte ROWS = 4; //nb lignes | + | |
- | const byte COLS = 3; //nb colonnes | + | |
- | char keys[ROWS][COLS] = { | + | |
- | {' | + | |
- | {' | + | |
- | {' | + | |
- | {' | + | |
- | }; | + | |
- | byte rowPins[ROWS] = {46, 47, 48, 49}; //4 pin | + | |
- | byte colPins[COLS] = {50, 51, 52}; //3 pin | + | |
- | Keypad keypad = Keypad( makeKeymap(keys), | + | |
- | const int agitateur = 26; //1 pin | + | {{: |
- | //capteur | ||
- | const int s0 = 18; | ||
- | const int s1 = 19; | ||
- | const int s2 = 22; | ||
- | const int s3 = 21; | ||
- | const int out = 20; | ||
- | /*int rouge = 0; | ||
- | int vert = 0; | ||
- | int bleu = 0; */ | ||
- | int couleur[3]; | ||
- | |||
- | //moteur | ||
- | int delaylegnth = 5; // Change la vitesse du pousse seringue, plus delaylgnth est élévé, | ||
- | int fincourse = 1023; // Variable du capteur de fin de course | ||
- | |||
- | const float vpas = 0.268; | ||
- | |||
- | |||
- | void color(){ | ||
- | digitalWrite(s2, | ||
- | digitalWrite(s3, | ||
- | //count OUT, pRed, RED | ||
- | couleur[0] = pulseIn(out, | ||
- | digitalWrite(s3, | ||
- | //count OUT, pBLUE, BLUE | ||
- | couleur[1] = pulseIn(out, | ||
- | digitalWrite(s2, | ||
- | //count OUT, pGreen, GREEN | ||
- | couleur[2] = pulseIn(out, | ||
- | } | ||
- | |||
- | /* | ||
- | digitalRead(out) == HIGH ? LOW : HIGH | ||
- | est équivalent à | ||
- | if (led_state == HIGH) | ||
- | | ||
- | else | ||
- | | ||
- | */ | ||
- | |||
- | |||
- | |||
- | |||
- | int pousse_seringue(int nbpas) | ||
- | // int -> none | ||
- | // Fait tourner le moteur du pousse seringue de nbpas pas. | ||
- | { | ||
- | if (nbpas < 0) | ||
- | { | ||
- | for (int i=0; i< (-nbpas / 4); i++) | ||
- | { | ||
- | digitalWrite(9, | ||
- | digitalWrite(8, | ||
- | |||
- | digitalWrite(12, | ||
- | analogWrite(3, | ||
- | | ||
- | delay(delaylegnth); | ||
- | | ||
- | digitalWrite(9, | ||
- | digitalWrite(8, | ||
- | |||
- | digitalWrite(13, | ||
- | analogWrite(11, | ||
- | | ||
- | delay(delaylegnth); | ||
- | | ||
- | digitalWrite(9, | ||
- | digitalWrite(8, | ||
- | |||
- | digitalWrite(12, | ||
- | analogWrite(3, | ||
- | | ||
- | delay(delaylegnth); | ||
- | | ||
- | digitalWrite(9, | ||
- | digitalWrite(8, | ||
- | |||
- | digitalWrite(13, | ||
- | analogWrite(11, | ||
- | | ||
- | delay(delaylegnth); | ||
- | } | ||
- | } | ||
- | |||
- | if (nbpas > 0) | ||
- | { | ||
- | for (int i=0; i< (nbpas / 4); i++) | ||
- | { | ||
- | digitalWrite(9, | ||
- | digitalWrite(8, | ||
- | |||
- | digitalWrite(12, | ||
- | analogWrite(3, | ||
- | | ||
- | delay(delaylegnth); | ||
- | | ||
- | digitalWrite(9, | ||
- | digitalWrite(8, | ||
- | |||
- | digitalWrite(13, | ||
- | analogWrite(11, | ||
- | | ||
- | delay(delaylegnth); | ||
- | | ||
- | digitalWrite(9, | ||
- | digitalWrite(8, | ||
- | |||
- | digitalWrite(12, | ||
- | analogWrite(3, | ||
- | | ||
- | delay(delaylegnth); | ||
- | | ||
- | digitalWrite(9, | ||
- | digitalWrite(8, | ||
- | |||
- | digitalWrite(13, | ||
- | analogWrite(11, | ||
- | | ||
- | delay(delaylegnth); | ||
- | } | ||
- | } | ||
- | } | ||
- | |||
- | |||
- | void initialisation_pousse_seringue() | ||
- | /*lance l' | ||
- | Branchements du capteur de fin de course : | ||
- | - fil rouge : 5V | ||
- | - fil jaune : GND | ||
- | - fil blanc : pin A15 */ | ||
- | { | ||
- | lcd.clear(); | ||
- | lcd.print(" | ||
- | |||
- | while (fincourse != 0) | ||
- | { | ||
- | pousse_seringue(-10); | ||
- | Serial.println(fincourse); | ||
- | fincourse = analogRead(A14); | ||
- | | ||
- | } | ||
- | // | ||
- | lcd.clear(); | ||
- | lcd.print(" | ||
- | lcd.setCursor(0, | ||
- | lcd.print(" | ||
- | bool eta = false; | ||
- | while (eta != true){ | ||
- | char touche = keypad.getKey(); | ||
- | if (touche == '#' | ||
- | eta = true; | ||
- | } | ||
- | } | ||
- | lcd.clear(); | ||
- | lcd.print(" | ||
- | pousse_seringue(2200); | ||
- | lcd.clear(); | ||
- | } | ||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | void setup() { | ||
- | |||
- | pinMode(A15, | ||
- | |||
- | pinMode(26, | ||
- | digitalWrite(agitateur, | ||
- | delay(100); | ||
- | digitalWrite(agitateur, | ||
- | | ||
- | //establish motor direction toggle pins | ||
- | pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards??? | ||
- | pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards??? | ||
- | | ||
- | //establish motor brake pins | ||
- | pinMode(9, OUTPUT); //brake (disable) CH A | ||
- | pinMode(8, OUTPUT); //brake (disable) CH B | ||
- | Serial.begin(9600); | ||
- | |||
- | |||
- | lcd.begin(16, | ||
- | lcd.print(" | ||
- | delay(100); | ||
- | lcd.clear(); | ||
- | keypad.setDebounceTime(20); | ||
- | // | ||
- | |||
- | //capteur | ||
- | pinMode(s0, OUTPUT); | ||
- | pinMode(s1, OUTPUT); | ||
- | pinMode(s2, OUTPUT); | ||
- | pinMode(s3, OUTPUT); | ||
- | pinMode(out, | ||
- | digitalWrite(s0, | ||
- | digitalWrite(s1, | ||
- | |||
- | Serial.begin(9600); | ||
- | |||
- | } | ||
- | |||
- | |||
- | |||
- | void loop(){ | ||
- | |||
- | |||
- | //choix couleur | ||
- | /*bool eta = false; | ||
- | char couleur = ' | ||
- | lcd.print(" | ||
- | lcd.setCursor(0, | ||
- | while (eta != true){ | ||
- | char touche = keypad.getKey(); | ||
- | if (touche != NO_KEY){ | ||
- | lcd.print(touche); | ||
- | couleur = touche; | ||
- | } | ||
- | if ((couleur = ' | ||
- | if (touche = '#' | ||
- | eta = true; | ||
- | } | ||
- | } | ||
- | } | ||
- | eta=false; | ||
- | lcd.clear(); | ||
- | |||
- | |||
- | |||
- | |||
- | bool eta = false; | ||
- | String co = String(); | ||
- | String vo = String(); | ||
- | String vt = String(); | ||
- | String fc = String(); | ||
- | |||
- | lcd.print(" | ||
- | lcd.setCursor(0, | ||
- | while (eta != true){ | ||
- | char touche = keypad.getKey(); | ||
- | if (touche == ' | ||
- | fc = ""; | ||
- | lcd.clear(); | ||
- | lcd.print(" | ||
- | lcd.setCursor(0, | ||
- | } | ||
- | else if (touche != NO_KEY && touche != '#' | ||
- | fc += touche; | ||
- | lcd.setCursor(0, | ||
- | lcd.print(fc); | ||
- | } | ||
- | if (touche == '#' | ||
- | eta=true; | ||
- | } | ||
- | } | ||
- | lcd.clear(); | ||
- | eta=false; | ||
- | |||
- | lcd.print(" | ||
- | lcd.setCursor(0, | ||
- | while (eta != true){ | ||
- | char touche = keypad.getKey(); | ||
- | if (touche == ' | ||
- | co = ""; | ||
- | lcd.clear(); | ||
- | lcd.print(" | ||
- | lcd.setCursor(0, | ||
- | } | ||
- | else if (touche != NO_KEY && touche != '#' | ||
- | co += touche; | ||
- | lcd.setCursor(0, | ||
- | lcd.print(co); | ||
- | } | ||
- | if (touche == '#' | ||
- | eta=true; | ||
- | } | ||
- | } | ||
- | lcd.clear(); | ||
- | eta=false; | ||
- | |||
- | |||
- | |||
- | |||
- | lcd.print(" | ||
- | lcd.setCursor(0, | ||
- | while (eta != true){ | ||
- | char touche = keypad.getKey(); | ||
- | if (touche == ' | ||
- | vo = ""; | ||
- | lcd.clear(); | ||
- | lcd.print(" | ||
- | lcd.setCursor(0, | ||
- | } | ||
- | else if (touche != NO_KEY && touche != '#' | ||
- | vo += touche; | ||
- | lcd.setCursor(0, | ||
- | lcd.print(vo); | ||
- | } | ||
- | if (touche == '#' | ||
- | eta=true; | ||
- | } | ||
- | } | ||
- | lcd.clear(); | ||
- | eta=false; | ||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | / | ||
- | while (opto < 50) | ||
- | { | ||
- | pousse_seringue(-12); | ||
- | opto = analogRead(A0); | ||
- | } | ||
- | while (opto > 50) | ||
- | { | ||
- | opto = analogRead(A0); | ||
- | } | ||
- | pousse_seringue(1200); | ||
- | |||
- | while(eta==false){ | ||
- | char touche = keypad.getKey(); | ||
- | if (touche == '#' | ||
- | eta = true; | ||
- | } | ||
- | pousse_seringue(-12); | ||
- | } | ||
- | pousse_seringue(100); | ||
- | lcd.clear(); | ||
- | eta = false;*/ | ||
- | |||
- | |||
- | initialisation_pousse_seringue(); | ||
- | |||
- | |||
- | lcd.print(" | ||
- | while (eta != true){ | ||
- | char touche = keypad.getKey(); | ||
- | if (touche == '#' | ||
- | eta = true; | ||
- | } | ||
- | } | ||
- | |||
- | int sommemax = 0; | ||
- | int sommecour = 0; | ||
- | int diff[3]; | ||
- | int prec[3]; | ||
- | color(); | ||
- | for(int i=0; i<3; i++){ | ||
- | prec[i] = couleur[i]; | ||
- | } | ||
- | |||
- | lcd.clear(); | ||
- | digitalWrite(agitateur, | ||
- | int compte=0; | ||
- | char touche = ' | ||
- | eta = false; | ||
- | while (eta != true){ | ||
- | char touche = keypad.getKey(); | ||
- | if (touche == '#' | ||
- | eta = true;} | ||
- | | ||
- | Serial.print(" | ||
- | color(); | ||
- | | ||
- | for(int i=0; i<3; i++){ | ||
- | diff[i] = abs(prec[i]-couleur[i]); | ||
- | } | ||
- | for(int i=0;i<3; i++){ | ||
- | prec[i] = couleur[i]; | ||
- | } | ||
- | sommecour = diff[0] + diff[1] + diff[2]; | ||
- | if (sommecour > sommemax){ | ||
- | sommemax = sommecour; | ||
- | } | ||
- | / | ||
- | lcd.print(" | ||
- | lcd.print(couleur[0], | ||
- | lcd.print(" | ||
- | lcd.print(couleur[1], | ||
- | lcd.print(" | ||
- | lcd.print(couleur[2], | ||
- | lcd.setCursor(0, | ||
- | lcd.print(compte); | ||
- | lcd.print(' | ||
- | lcd.print(sommecour); | ||
- | lcd.print(' | ||
- | lcd.print(sommemax); | ||
- | pousse_seringue(50); | ||
- | compte ++; | ||
- | | ||
- | } | ||
- | |||
- | lcd.clear(); | ||
- | lcd.print(" | ||
- | delay(1000); | ||
- | lcd.clear(); | ||
- | |||
- | |||
- | //tofloat for all var | ||
- | float fcompte = (float) compte; | ||
- | |||
- | char floatbuf[32]; | ||
- | |||
- | co.toCharArray(floatbuf, | ||
- | float fco = atof(floatbuf); | ||
- | vo.toCharArray(floatbuf, | ||
- | float fvo = atof(floatbuf); | ||
- | fc.toCharArray(floatbuf, | ||
- | float ffc = atof(floatbuf); | ||
- | |||
- | |||
- | |||
- | float mes = ffc*(fco*fcompte*10*vpas)/ | ||
- | lcd.print(" | ||
- | lcd.setCursor(0, | ||
- | lcd.print(mes); | ||
- | lcd.print(" | ||
- | |||
- | |||
- | eta = false; | ||
- | while (eta != true){ | ||
- | char touche = keypad.getKey(); | ||
- | if (touche == '#' | ||
- | eta = true; | ||
- | lcd.clear(); | ||
- | } | ||
- | } | ||
- | | ||
- | } | ||