• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Auto-Turret (problem z przyciskami on/off)
#1
Witam,

Ostatnimi czasy w godzinach nocnych mam sporo nieproszonych gości, błąkających się po moim podwórku i najwyraźniej szukających czegoś, co mogłoby im się akurat przydać (if you know what i have mean). Dlatego postanowiłem zrobić mały "system ostrzegawczy" przed takimi przybyszami. 

ZARYS: Ogólnym pomysłem było zrobienie czegoś na wzór monitoringu, z możliwością automatycznego śledzenia poruszających się obiektów (w trybie auto lub manualnie), nagrywanie, zapalania oświetlenia, itp. itd.
No i  świetnie. Same funkcje, które na razie wprowadziłem działają.

PROBLEM: Problem pojawił się natomiast w trakcie, gdy zaimplementowałem możliwość wyboru jednego z programów (auto, manual, wyłączenie systemu). Do wyboru chciałem użyć pilota radiowego 315 Mhz, o zasięgu 50m (dostępny w sklepach z rzeczami do Arduino). I co się okazało. Raz wciśnięty przycisk (który w założeniu miał być on/off) powoduje, wybór dostępnego programu (roboczo nazwany MODE) i "zacięciu się" programu w tej jednej pętli, tak że nie mogę zmienić MODE na inny, tylko jestem zmuszony resetować arduino.
Jak poprawnie napisać taką pętle, bo ja już rozkładam ręce. Próbowałem chyba wszystkiego i wciąż program albo zawiesza się w pętli, albo wykonuje tylko raz.

PROGRAM:
Kod:
#include<Servo.h> //inculde servo motor libriry
int Trigleft = 6; //connect trig pin of left ultrasonic to arduino pin A0
int Echoleft = A1; //connect echo pin of left ultrasonic to arduino pin A1
int TrigRight = A2; //connect trig pin of right ultrasonic to arduino pin A2
int EchoRight = A3; //connect echo pin of right ultrasonic to arduino pin A3
int TrigUP = 11; //connect trig pin of upper ultrasonic to arduino pin A4
int EchoUP = A5; //connect echo pin of upper ultrasonic to arduino pin A5
int TrigDown = 7; //connect trig pin of down ultrasonic to arduino pin 7
int EchoDown = 8; //connect echo pin of down ultrasonic to arduino pin 8
int LReading; //memory location to store Left ultrasonic Reading
int RReading; //memory location to store right ultrasonic Reading
int UReading; // memory location to store Upper ultrasonic Reading
int DReading; //memory location to store Down ultrasonic Reading
Servo Zservo; //create servo object for vertical motor
Servo XYservo; //create servo object for Horizontal motor
int Zpos = 90; //set servo start angle for vertical motor  at 90 degree
int XYpos = 90; // set servo start angle for horizontal motor at 90 degree
int SwitchA = 9;
int SwitchB = 10;
int SwitchC = 12;
int SwitchD = 13;
int servoVal;
int joyXY = A0;
int joyZ = A4;
int relaya;
int relayb;
int relayc;
int relayd;



void setup() {
Serial.begin(9600); //start serial communication
  Zservo.attach(5); //attach Vertical motor to arduino pin 5
  XYservo.attach(3); //attach Horizontal motor to arduino pin 3
  Zservo.write(Zpos); //vertical servo motor move to start point
  XYservo.write(XYpos); // Horizontal servo motor move to start point
  pinMode(SwitchA, INPUT); //Auto-Mode-ARmed
  pinMode(SwitchB, INPUT); // Auto-Mode-UNArmed
  pinMode(SwitchC, INPUT); // Manual-Mode-Armed
  pinMode(SwitchD, INPUT); // TRACKING - OFF
  relaya= LOW;
  relayb= LOW;
  relayc= LOW;
  relayd= LOW;
 
}

void loop() {

// Auto-Mode-UNArmed / ARmed
if (digitalRead(SwitchA)==HIGH){
relaya=!relaya;
while (digitalRead(relaya)==HIGH){
  LReading = GetSonarValue(Trigleft, Echoleft); //get left ultrasonic reading
  Serial.print("Left Value");
  Serial.println(LReading);
  RReading = GetSonarValue(TrigRight, EchoRight); //get right ultrasonic reading
  Serial.print("Right Value");
  Serial.println(RReading);

  UReading = GetSonarValue(TrigUP, EchoUP); //get upper ultrasonic reading
  Serial.print("UP Value");
  Serial.println(UReading);

  DReading = GetSonarValue(TrigDown, EchoDown); //get down ultrasonic reading
  Serial.print("Down Value");
  Serial.println(DReading);
  if (LReading <= 20 && RReading <= 20 && UReading <= 20 && DReading <= 20)
  {
    //Move Right and Down
    while (LReading > RReading && UReading > DReading)
    {
      Zpos = Zpos - 1;
      XYpos = XYpos + 1;
      Zservo.write(Zpos);
      XYservo.write(XYpos);
      break;
    }
    //Move Left and Down
    while (LReading < RReading && UReading > DReading)
    {
      Zpos = Zpos - 1;
      XYpos = XYpos - 1;
      Zservo.write(Zpos);
      XYservo.write(XYpos);
      break;
    }
    //Move Right and UP
    while (LReading > RReading & UReading < DReading)
    {
      Zpos = Zpos + 1;
      XYpos = XYpos + 1;
      Zservo.write(Zpos);
      XYservo.write(XYpos);
      break;
    }
    //Move Left and UP
    while (LReading < RReading && UReading < DReading)
    {
      Zpos = Zpos + 1;
      XYpos = XYpos - 1;
      Zservo.write(Zpos);
      XYservo.write(XYpos);
      break;
    }
    //Move Left
    while (LReading < RReading && UReading == DReading)
    {
      XYpos = XYpos - 1;
      XYservo.write(XYpos);
      Zservo.write(Zpos);
      break;
    }
    //Move Right
    while (LReading > RReading && UReading == DReading)
    {
      XYpos = XYpos + 1;
      XYservo.write(XYpos);
      Zservo.write(Zpos);
      break;
    }
    //Move UP
    while (LReading == RReading && UReading < DReading)
    {
      Zpos = Zpos + 1;
      Zservo.write(Zpos);
      XYservo.write(XYpos);
      break;
    }
    //Move Down
    while (LReading == RReading && UReading > DReading)
    {
      Zpos = Zpos - 1;
      Zservo.write(Zpos);
      XYservo.write(XYpos);
      break;
    }
}
Serial.print("AUTO-MODE! ");
}
}
//Manual-Mode-Armed
if (digitalRead(SwitchB)==HIGH){
relayb=!relayb;
while (digitalRead(relayb)==HIGH){
servoVal = analogRead(joyXY);
  servoVal = map(servoVal, 0, 1023, 0, 180);
  XYservo.write(servoVal);
  servoVal = analogRead(joyZ);
  servoVal = map(servoVal, 0, 1023, 0, 180);
  Zservo.write(servoVal);
  Serial.print("MANUAL-MODE-ARMED! ");
  delay(15);
}
}
// OFF
if (digitalRead(SwitchC)==HIGH){
relayc=!relayc;
while (digitalRead(relayc)==HIGH){
  Serial.print(relayc);
  Serial.print("System wyłączony! ");
//Void
}
}
}
int GetSonarValue(int TrigPin, int Echopin)
{
  int distance, duration;
  pinMode(TrigPin, OUTPUT);
  digitalWrite(TrigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(TrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(TrigPin, LOW);
  pinMode(Echopin, INPUT);

  duration = pulseIn(Echopin, HIGH);
  distance = duration * 0.034 / 2;
  delay(10);
  return distance;
}
 
Odpowiedź
#2
Pozbaw się "while" użyłeś tego tyle ze sam się pogubiłem...

Nie nakazuj mu czekać na coś niech procesora zaiwania w kółko pętli...
Arduino zostało wymyślone po to, by robić dobrze jedną prostą rzecz – migać diodą. 
 
Odpowiedź
#3
No właśnie w jednej z prób pozbyłem się while. Skutek tego był taki, że proces wykonywał się tylko raz.
Dla pewności powiem, że chodzi o pętle pod komentarzami //OFF; //Manual-Mode-Armed i //Auto-Mode- UnArmed
 
Odpowiedź
  


Skocz do:


Przeglądający: 1 gości