Skocz do zawartości

Kurs Arduino - #6 - kontynuacja UART, serwomechanizmy


Komentator

Pomocna odpowiedź

1 godzinę temu, Pawel56 napisał:

@Danyeru Dzięki wielkie! Pomogło 😄
Próbowałem to zamieniać ale chyba nie potrzebnie oba parametry zamieniałem miejscami 😉
Pozdrawiam 🙂

Yup, zmieniając oba dalej mapowałeś ten sam zakres odczytu na te same wartości pozycji serwa.

  • Lubię! 1
Link do komentarza
Share on other sites

#include <Servo.h> //Library responsible for servo

Servo servomechanism; //Object that refer to servo
byte position = 180; //Current servo position within 0-180
int previousPosition = 0;
int setMinValue = 0; //Minimal value calibrate
int setMaxValue = 0; //Maximum value calibrate
int counterPress = 0;

void setup() {
  servomechanism.attach(11); //Servo connected to pin 11
  Serial.begin(9600); //Transmission speed setting
  pinMode(10, INPUT_PULLUP); //Button pin mode
}

void loop() {
  int sensorRead = analogRead(A5); //Sensor value read

  position = map(sensorRead, setMinValue, setMaxValue, 180, 0); //Change to servo position

  if(digitalRead(10) == HIGH && counterPress == 0) {

    if (abs(position-previousPosition) > 5) { //Check whether positions are different more than 5 degrees
      servomechanism.write(position); //Servo move
      previousPosition = position; //Remember current position as previous
      }
      Serial.print("Servo: "); //Send a message to terminal
      Serial.println(position); //Send a message to terminal
      Serial.print("Sensor: "); //Send a message to terminal
      Serial.println(sensorRead); //Send a message to terminal
      delay(300); //Delay for better effect
  }
    while(digitalRead(10) == LOW) {//while button press
    counterPress = counterPress + 1;
    if(counterPress == 1) {//Calibrating min value start
      Serial.println("Set min. value by remove all light sources from sensor and wait 5 sec.");
      delay(5000);
      setMinValue = sensorRead;
      Serial.print("setMinValue is ");
      Serial.println(setMinValue);
      }
      if(counterPress == 2) {//Calibrating max value start
      Serial.println("Set max. value by put light on sensor and wait 5 sec.");
      delay(5000);
      setMaxValue = sensorRead;
      Serial.print("setMaxValue is ");
      Serial.println(setMaxValue);
      counterPress = 0;
    }
  }
}

Czy jest opcja napisania programu tak aby istniała możliwość zapisu ustawionych po kalibracji wartości max i min w pamięci, tak aby po odłączeniu zasilania od arduino i ponownym jego podłączeniu urządzenie pamiętało skalibrowane wartości max i min ?

20240304_171707.jpg

20240304_171932.jpg

20240221_231345.jpg

Link do komentarza
Share on other sites

Zarejestruj się lub zaloguj, aby ukryć tę reklamę.
Zarejestruj się lub zaloguj, aby ukryć tę reklamę.

jlcpcb.jpg

jlcpcb.jpg

Produkcja i montaż PCB - wybierz sprawdzone PCBWay!
   • Darmowe płytki dla studentów i projektów non-profit
   • Tylko 5$ za 10 prototypów PCB w 24 godziny
   • Usługa projektowania PCB na zlecenie
   • Montaż PCB od 30$ + bezpłatna dostawa i szablony
   • Darmowe narzędzie do podglądu plików Gerber
Zobacz również » Film z fabryki PCBWay

#include <avr/eeprom.h> //Library of data saving
#include <Servo.h> //Library responsible for servo

Servo servomechanism; //Object that refer to servo
byte position = 180; //Current servo position within 0-180
int previousPosition = 0;
int setMinValue = 0; //Minimal value calibrate
int setMaxValue = 0; //Maximum value calibrate
int counterPress = 0;

void setup() {
  servomechanism.attach(11); //Servo connected to pin 11
  Serial.begin(9600); //Transmission speed setting
  pinMode(10, INPUT_PULLUP); //Button pin mode
  eeprom_read_block(&setMinValue,0,2);
  Serial.print("setMinValue is ");
  Serial.println(setMinValue);
  eeprom_read_block(&setMaxValue,2,2);
  Serial.print("setMaxValue is ");
  Serial.println(setMaxValue);
}

void loop() {
  int sensorRead = analogRead(A5); //Sensor value read

  position = map(sensorRead, setMinValue, setMaxValue, 180, 0); //Change to servo position

  if(digitalRead(10) == HIGH && counterPress == 0) {

    if (abs(position-previousPosition) > 5) { //Check whether positions are different more than 5 degrees
      servomechanism.write(position); //Servo move
      previousPosition = position; //Remember current position as previous
      }
      Serial.print("Servo: "); //Send a message to terminal
      Serial.println(position); //Send a message to terminal
      Serial.print("Sensor: "); //Send a message to terminal
      Serial.println(sensorRead); //Send a message to terminal
      delay(300); //Delay for better effect
  }
    while(digitalRead(10) == LOW) {//while button press
    counterPress = counterPress + 1;
    if(counterPress == 1) {//Calibrating min value start
      Serial.println("Set min. value by remove all light sources from sensor and wait 5 sec.");
      delay(5000);
      setMinValue = sensorRead;
      eeprom_write_block(&setMinValue,0,2);
      Serial.print("setMinValue is ");
      Serial.println(setMinValue);
      }
      if(counterPress == 2) {//Calibrating max value start
      Serial.println("Set max. value by put light on sensor and wait 5 sec.");
      delay(5000);
      setMaxValue = sensorRead;
      Serial.print("setMaxValue is ");
      Serial.println(setMaxValue);
      eeprom_write_block(&setMaxValue,2,2);
      counterPress = 0;
    }
  }
}

Tym razem kod z możliwością zapisu w pamięci wartości max. i min.

  • Lubię! 1
Link do komentarza
Share on other sites

(edytowany)

Bardzo ładnie @ARDIP. Nie pomyślałem o użyciu fizycznego przycisku do kalibracji 🙂
Ja wybrałem pewnie bardziej pokraczną wersję z komunikacją poprzez UART

 

#include <Servo.h>

Servo microservo;
byte position = 0;
int previousPosition = 0;
String lowestSensorRead = "";
String highestSensorRead = "";
bool isCalibrated = false;


void setup() {
  microservo.attach(11);
  Serial.begin(9600);

  Serial.println();
  Serial.println("Kalibracja fotorezystora");
    
  while(!isCalibrated){
    Serial.println("Zasłoń zupełnie fotorezystor na płytce. Jak to zrobisz to wpisz w terminalu: r");
    while(Serial.available() == 0 && Serial.readStringUntil('\n') != "r"){}
      lowestSensorRead = analogRead(A5);

      Serial.println("Lowest Sensor Read: " + String(lowestSensorRead));

    Serial.println("Przystaw do fotorezystora mocne źródło światała. Jak to zrosiz to wpisz w terminalu: r");  
    while(Serial.available() == 0 && Serial.readStringUntil('\n') != "r"){}
      highestSensorRead = analogRead(A5);

      Serial.println("Highest Sensor Read: " + String(highestSensorRead));
      isCalibrated = true;
  }

}

void loop() {


  int sensorRead = analogRead(A5);
  position = map(sensorRead, highestSensorRead.toInt(), lowestSensorRead.toInt(), 0, 180);

  if(abs(position-previousPosition) > 5){
    microservo.write(position);
    previousPosition = position;
  }

  Serial.println(sensorRead);
  delay(300);

}

 

Edytowano przez FTNewbie
  • Lubię! 2
Link do komentarza
Share on other sites

Dołącz do dyskusji, napisz odpowiedź!

Jeśli masz już konto to zaloguj się teraz, aby opublikować wiadomość jako Ty. Możesz też napisać teraz i zarejestrować się później.
Uwaga: wgrywanie zdjęć i załączników dostępne jest po zalogowaniu!

Anonim
Dołącz do dyskusji! Kliknij i zacznij pisać...

×   Wklejony jako tekst z formatowaniem.   Przywróć formatowanie

  Dozwolonych jest tylko 75 emoji.

×   Twój link będzie automatycznie osadzony.   Wyświetlać jako link

×   Twoja poprzednia zawartość została przywrócona.   Wyczyść edytor

×   Nie możesz wkleić zdjęć bezpośrednio. Prześlij lub wstaw obrazy z adresu URL.

×
×
  • Utwórz nowe...

Ważne informacje

Ta strona używa ciasteczek (cookies), dzięki którym może działać lepiej. Więcej na ten temat znajdziesz w Polityce Prywatności.