Stránka 1 z 2

Pomoc s chybami v programu

Napsal: 13 lis 2019, 23:25
od Spider 1970
Zdravím
Poprosil bych ještě jednou o opravu chyb v programu.Najde se zde někdo kdo mi tento program uvedl do funkčního stavu?

Kód: Vybrat vše

[code]

#include <LiquidCrystal.h>
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 ); //Pins used by LCD
// Global variables
int adc_key_val[5] = {50, 200, 400, 600, 800 }; // These are the values returned by the adc for different button presses
int NUM_KEYS = 5;
int adc_key_in;
int key = -1;
int oldkey = -1;
int jog_speed;
int rail_direction = 0;
//Pins that will be used to control the rail and the camera
int dir_pin = 11; // 10 clashes with backlight pin! Solution is probably to move camer_pin to another output..
int drive_pin = 12;
int enable_pin = 13;
int camera_pin = 15;
int flash_pin = 16;
int number_photos = 0;
int distance = 0;
int delay_time = 0;
int k = 0;
int keep[4] = {0, 0, 0, 0}; // Only really need four members to array because not using 0th member!

//
//
// This runs once and sets up the display, signs on, gives basic info etc.
//
//
void setup()
{
  pinMode(enable_pin, OUTPUT);
  digitalWrite(enable_pin, HIGH); // just in case - turn motor off
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
  //1234567890123456
  lcd.print(& amp; quot; MadBoffin Labs & amp; amp; quot;);
  lcd.setCursor(0, 1);
  lcd.print(& amp; quot; 5th Oct 2014 & amp; amp; quot;);
  delay(1500);
  lcd.clear();
  lcd.setCursor(0, 0);
  //1234567890123456
  lcd.print(& amp; quot; STEPPER & amp; amp; quot;);
  lcd.setCursor(0, 1);
  //1234567890123456
  lcd.print(& amp; quot; RAIL & amp; amp; quot;);
  delay (1500);
  for (char k = 0; k & amp; amp; lt; 16; k++)
    lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(& amp; quot; Use buttons to & amp; amp; quot;);
  lcd.setCursor(0, 1);
  //1234567890123456
  lcd.print(& amp; quot; change values & amp; amp; quot;);
  delay (1500);
  lcd.clear();
  lcd.setCursor(0, 0);
  //1234567890123456
  lcd.print(& amp; quot; To end entries & amp; amp; quot;);
  lcd.setCursor(0, 1);
  lcd.print(& amp; quot; press & amp; amp; lt; Select & amp; amp; gt; & amp; quot;);
  delay (1500);
  lcd.clear();
}
//
//
// This loops for ever and always returns asking whether to jog or stack
//
//
void loop()
{
  pinMode(enable_pin, OUTPUT);
  digitalWrite(enable_pin, HIGH); // motor off at return from stacker or jogger
  // 1234567890123456
  lcd.print(& amp; quot; Stack or Jog ? & amp; quot;);
  lcd.setCursor(0, 1);
  lcd.print(& amp; quot; L = Stack, R = Jog & amp; amp; quot;);
  //
  which_button(key);
  switch (key) {
    case (3):
      lcd.clear();
      lcd.setCursor(0, 0);
      // 1234567890123456
      lcd.print(& amp; quot; Calling Stacker & amp; amp; quot;);
      lcd.setCursor(0, 1);
      lcd.print(& amp; quot; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; quot;);
      delay(1500);
      digitalWrite(enable_pin, LOW); // motor on
      stacker();
      break;
    case (0):
      lcd.clear();
      lcd.setCursor(0, 0);
      // 1234567890123456
      lcd.print(& amp; quot; Calling Jogger & amp; amp; quot;);
      lcd.setCursor(0, 1);
      lcd.print(& amp; quot; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; quot;);
      delay(1500);
      digitalWrite(enable_pin, LOW); //motor on
      jogger();
      break;
  }
}

//
//
//ROUTINE - stacker() gets button presses and adds up values ascribed to them to give an accumulated answer
//
//
void stacker()
{
  int stop_flag_dir_stack = -1;
  int stop_flag = 1;
  int long accumulator = 0;
  int function = 1;
  rail_direction = 0;
  number_photos = 0;
  distance = 0;
  delay_time = 0;
  lcd.clear();
  lcd.setCursor(0, 0);
  // 1234567890123456
  lcd.print(& amp; quot; DIRECTION ? & amp; quot;);
  lcd.setCursor(0, 1);
  lcd.print(& amp; quot; Back = L Forward = R & amp; amp; quot;);
  //Get a keypress and debounce it.
  do {
    which_button(key);
    switch (key) {
      case 0: //right button
        rail_direction = 1;
        lcd.clear();
        // 1234567890123456
        lcd.setCursor(0, 0);
        lcd.print(& amp; quot; Rail will move: & amp; quot;);
        lcd.setCursor(0, 1);
        // 1234567890123456
        lcd.print(& amp; quot; Forwards! & amp; quot;);
        delay(1000);
        stop_flag_dir_stack = 1;
        break;
      case 3: //left button
        rail_direction = -1;
        lcd.clear();
        lcd.setCursor(0, 0);
        // 1234567890123456
        lcd.print(& amp; quot; Rail will move: & amp; quot;);
        lcd.setCursor(0, 1);
        // 1234567890123456
        lcd.print(& amp; quot; Backwards! & amp; quot;);
        delay(1000);
        stop_flag_dir_stack = 1;
        break;
    }
  } while (stop_flag_dir_stack != 1);
  do {
    lcd.clear();
    lcd.setCursor(0, 0);
    switch (function) {
      case 1:
        //1234567890123456
        lcd.print(& amp; quot; NOS OF PHOTOS ? & amp; quot;);
        break;
      case 2:
        lcd.print(& amp; quot; DISTANCE IN uM ? & amp; quot;);
        break;
      case 3:
        lcd.print(& amp; quot; DELAY IN mS ? & amp; quot;);
        break;
    }
    which_button(key);
    // Turn button presses into values
    switch (key) {
      case 0:
        accumulator = accumulator - 1;
        break;
      case 3:
        accumulator = accumulator + 10;
        break;
      case 1:
        accumulator = accumulator + 100;
        break;
      case 2:
        accumulator = accumulator - 10;
        break;
      case 4:
        stop_flag = stop_flag + 1;
        keep[function] = accumulator;
        lcd.setCursor(0, 1);
        // 1234567890123456
        lcd.print(& amp; quot; & amp; quot;);
        lcd.setCursor(0, 1);
        lcd.print(& amp; quot; Using: & amp; quot;);
        lcd.print(keep[function]);
        delay(1000);
        function = function + 1;
        accumulator = 0;
        break;
    }
    // Sets lower limit
    if (accumulator & amp; amp; lt; = 0) {
      accumulator = 0;
    }
    if (function != 4) {
      lcd.setCursor(0, 1);
      // 1234567890123456
      lcd.print(& amp; quot; & amp; quot;);
      lcd.setCursor(0, 1);
      lcd.print(accumulator);
      delay(100);
    }
  } while (stop_flag & amp; amp; lt; = 3);
  number_photos = keep[1];
  keep[1] = 0;
  distance = keep[2];
  keep[2] = 0;
  delay_time = keep[3];
  keep[3] = 0;
  motor_driver(rail_direction, number_photos, distance, delay_time);
}

//
//
// ROUTINE - which_button () returns debounced key
//
//
int which_button (int a)
{
  adc_key_in = analogRead(0);
  key = get_key(adc_key_in);
  if (key != oldkey) {
    delay(30);
    adc_key_in = analogRead(0);
    key = get_key(adc_key_in);
    oldkey = key; // position?
    return (key);
  }
}
//
//
// Convert ADC value to key number/button press
//
//
int get_key(unsigned int input)
{
  int k;
  for (k = 0; k < NUM_KEYS; k++))
  {
    if (input & amp; amp; lt; adc_key_val[k])
    {
      return k;
    }
  }
  if (k & amp; amp; gt; = NUM_KEYS)k = -1; // No valid key pressed
return k;
}
//
//
// ROUTINE - jogger () gets buttons and attributes values to them.
// Then call jog_motor() to move the rail.
// 'Left' and 'Right' buttons will be slow jog forwards and backwards.
//
//
void jogger()
{
  // First tell user what the buttons do...
  int jog_stop_flag = 0;
  jog_speed = 0;
  lcd.clear();
  lcd.setCursor(0, 0);
  // 1234567890123456
  lcd.print(& amp; quot; Slow Jog: L and R & amp; amp; quot;);
  lcd.setCursor(0, 1);
  lcd.print(& amp; quot; Fast: Up and Down & amp; amp; quot;);
  delay(1500);
  lcd.clear();
  lcd.setCursor(0, 0);
  // 1234567890123456
  lcd.print(& amp; quot; Use & amp; amp; lt; Select & amp; amp; gt; & amp; quot;);
  lcd.setCursor(0, 1);
  lcd.print(& amp; quot; to exit to menu & amp; amp; quot;);
  delay(1500);
  lcd.clear();
  lcd.setCursor(0, 0);
  // 1234567890123456
  lcd.print(& amp; quot; OK ready to jog!& amp; quot;);
  //now get key presses
  do {
    which_button(key);
    // Now get speed and direction for jog and call jog_motor
    switch (key) {
      case 0:
        jog_speed = - 200;
        jog_motor(jog_speed);
        break;
      case 3:
        jog_speed = + 200;
        jog_motor(jog_speed);
        break;
      case 1:
        jog_speed = + 20;
        jog_motor(jog_speed);
        break;
      case 2:
        jog_speed = - 20;
        jog_motor(jog_speed);
        break;
      case 4:
        // stop jog
        jog_stop_flag = 1;
        break;
    }
  } while (jog_stop_flag != 1);
}
//
//
// ROUTINE jogs the motor via EasyDriver
//
//
int jog_motor(int a)
{
  pinMode(dir_pin, OUTPUT);
  pinMode(drive_pin, OUTPUT);
  pinMode(camera_pin, OUTPUT);
  digitalWrite(dir_pin, LOW);
  digitalWrite(drive_pin, LOW);
  // Set direction and then drive motor at with jog_speed microsecond delays between pulse phases
  if (jog_speed & amp; amp; lt; = 0) {
    digitalWrite(dir_pin, HIGH);
    jog_speed = jog_speed * -1;
  }
  digitalWrite(drive_pin, LOW);
  delayMicroseconds(jog_speed);
  digitalWrite(drive_pin, HIGH);
  delayMicroseconds(jog_speed);
}
//
//
// ROUTINE that moves the motor via EasyDriver and activates camera to produce a photo stack
//
//
int motor_driver(int a, int b, int c, int d)
{
  // This value is crucial for absolute accuracy and needs to be set by moving the rail and seeing how far it actually travels.
  float steps_per_micron = 0.78824; //1.25mm is 1600 steps (with 8 microsteps per step) so 0.78125 steps/micron!
  long int steps_between_photos;
  long int distance_between_photos;
  long int total_steps;
  int carry_direction;
  pinMode(dir_pin, OUTPUT);
  pinMode(drive_pin, OUTPUT);
  pinMode(camera_pin, OUTPUT);
  pinMode(flash_pin, OUTPUT);
  digitalWrite(dir_pin, LOW);
  digitalWrite(drive_pin, LOW);
  distance_between_photos = distance / (number_photos - 1);
  steps_between_photos = (float)distance_between_photos / steps_per_micron; // check loss of precision!
  lcd.clear();
  lcd.setCursor(0, 0);
  // 1234567890123456
  lcd.print(& amp; quot; Rail Direction ==1 & amp; amp; quot;);
  lcd.setCursor(0, 1);
  lcd.print(rail_direction);
  delay (1500);
  lcd.clear();
  lcd.setCursor(0, 0);
  // 1234567890123456
  lcd.print(& amp; quot; Number photos = & amp; quot;);
  lcd.setCursor(0, 1);
  lcd.print(number_photos);
  delay (1500);
  lcd.clear();
  lcd.setCursor(0, 0);
  // 1234567890123456
  lcd.print(& amp; quot; Distance = & amp; quot;);
  lcd.setCursor(0, 1);
  lcd.print(distance);
  delay (1500);
  lcd.clear();
  lcd.setCursor(0, 0);
  // 1234567890123456
  lcd.print(& amp; quot; Delay time = & amp; quot;);
  lcd.setCursor(0, 1);
  lcd.print(delay_time);
  delay (1500);
  //head off in the right direction!
  if (rail_direction == -1) {
    digitalWrite(dir_pin, LOW);
  }
  if (rail_direction == 1) {
    digitalWrite(dir_pin, HIGH);
  }
  //set camera_pin high - first photo is where the rail is now
  //tell operator taking first photo
  lcd.clear();
  lcd.setCursor(0, 0);
  // 1234567890123456
  lcd.print(& amp; quot; "Photo #: " 1 & amp; amp; quot;);
  lcd.setCursor(0, 1);
  lcd.print(& amp; quot; In stack of: & amp; quot;);
  lcd.print(number_photos);
  digitalWrite(camera_pin, HIGH);
  digitalWrite(flash_pin, HIGH);
  delay(100);
  digitalWrite(camera_pin, LOW);
  digitalWrite(flash_pin, LOW);
  for (int x_count_photos = 1; x_count_photos & amp; amp; lt; = number_photos - 1; x_count_photos++) {
    //move rail position by steps_between_photos
    //motor speed is set by delays....going slowly to avoid lost steps.....
    for (int y_count_steps = 1; y_count_steps & amp; amp; lt; = steps_between_photos; y_count_steps++) {
      digitalWrite(drive_pin, LOW);
      delayMicroseconds(200);
      digitalWrite(drive_pin, HIGH);
      delayMicroseconds(200);
    }
    delay(delay_time);
    //Tell user which photo is being taken
    lcd.clear();
    lcd.setCursor(0, 0);
    // 1234567890123456
    lcd.print(& amp; quot; "Photo #: " & amp; quot;);
    // take the photo and set flash_pin high with camera_pin long enough to trigger camera
    digitalWrite(camera_pin, HIGH);
    digitalWrite(flash_pin, HIGH);
    delay(100);
    digitalWrite(camera_pin, LOW);
    digitalWrite(flash_pin, LOW);
    lcd.print(x_count_photos + 1);
    lcd.setCursor(0, 1);
    lcd.print(& amp; quot; In stack of: & amp; quot;);
    lcd.print(number_photos);
  }
  delay(1000);// so you can see the last value for photo number
  //
  //Rewind the slider to the starting point by reversing rail_direction and going back total number of steps in stack
  //
  //Warn that rewind about to take place..
  lcd.clear();
  lcd.setCursor(0, 0);
  // 1234567890123456
  lcd.print(& amp; quot; Rewinding! & amp; quot;);
  lcd.setCursor(0, 1);
  lcd.print(& amp; quot; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; gt; & amp; quot;);
  carry_direction = rail_direction;
  if (carry_direction == -1) {
    digitalWrite(dir_pin, HIGH);
  }
  if (carry_direction == 1) {
    digitalWrite(dir_pin, LOW);
  }
  //digitalWrite(dir_pin, rail_direction);
  total_steps = (number_photos - 1) * steps_between_photos;
  for (int x_count_rewind = 1; x_count_rewind & amp; amp; lt; = total_steps; x_count_rewind ++) {
    digitalWrite(drive_pin, LOW);
    delayMicroseconds(100);
    digitalWrite(drive_pin, HIGH);
    delayMicroseconds(100);
  }
  lcd.clear();
}

[/code]

Děkuji všem kteří mi pomohou

Re: Pomoc s chybami v programu

Napsal: 14 lis 2019, 07:11
od ondraN
Asi by bylo dobré, se nejdříve seznámit se základy programovacího jazyka. Ten kód tady je naprosto nesmyslný.
Například tento pžíkaz for

Kód: Vybrat vše

  for (char k = 0; k & amp; amp; lt; 16; k++)
    lcd.clear();
má jednak moc parametrů a bude stále dokola jen opakovat smazání displeje. Jeho smysl mi naprosto uniká. A takových nesmyslů je tam nepočítaně. Kód zahoď, začni od něčeho jednoduchého, nejlépe podle nějaké rozumné knihy pro začátečníky a postupně zvyšuj svoje znalosti. Jedině to je cesta, jak se dobrat nějakého úspěchu.

Re: Pomoc s chybami v programu

Napsal: 14 lis 2019, 07:39
od pavel1tu
Ahoj,
pokud na to nespěcháš nemám problém se na to večer kouknout
a pak by se to doladilo třeba po víkendu - v pátek vedu kroužek a víkend jsem se rozhodl prospat ;)

Poprosím jen o jedno - popiš mi co od tohoto programu čekáš, popřípadě odkud jsi čerpal zdroj, co se má dít v sekci setup() a loop() - prostě co chceš aby to dělalo

Vysvětlíme si chyby a další věci ....

Re: Pomoc s chybami v programu

Napsal: 14 lis 2019, 08:06
od Spider 1970
pavel1tu píše:
14 lis 2019, 07:39
Ahoj,
pokud na to nespěcháš nemám problém se na to večer kouknout
a pak by se to doladilo třeba po víkendu - v pátek vedu kroužek a víkend jsem se rozhodl prospat ;)

Poprosím jen o jedno - popiš mi co od tohoto programu čekáš, popřípadě odkud jsi čerpal zdroj, co se má dít v sekci setup() a loop() - prostě co chceš aby to dělalo

Vysvětlíme si chyby a další věci ....
. Děkuji moc až budu doma u počítače tak ti vše vysvetlim. Opravdu dik

Re: Pomoc s chybami v programu

Napsal: 14 lis 2019, 08:19
od AstroMiK
ondraN píše:
14 lis 2019, 07:11
Například tento pžíkaz for

Kód: Vybrat vše

  for (char k = 0; k & amp; amp; lt; 16; k++)
    lcd.clear();

To jsou jen nějak zmršené HTML entity, které vznikly překopírováním kódu z nějakého webu.

Správně by to mělo být:

Kód: Vybrat vše

for (char k = 0; k < 16; k++)

Re: Pomoc s chybami v programu

Napsal: 14 lis 2019, 08:38
od KamilV
Ono to vypadá, že se Ti v tom kódu někde escapovaly html znaky. Z "&" se stalo "&", z "<" se stalo "<" atp.
Proto se for cyklus zdá být jako nesmyslný. Otázkou je, kde se tomu tak stalo, jestli až po publikování kódu zde na fóru, nebo už při kopírování někde z webu a takhle pošmodrchané jsi to vkopíroval do IDE...

Re: Pomoc s chybami v programu

Napsal: 14 lis 2019, 09:00
od Spider 1970
KamilV píše:
14 lis 2019, 08:38
Ono to vypadá, že se Ti v tom kódu někde escapovaly html znaky. Z "&" se stalo "&", z "<" se stalo "<" atp.
Proto se for cyklus zdá být jako nesmyslný. Otázkou je, kde se tomu tak stalo, jestli až po publikování kódu zde na fóru, nebo už při kopírování někde z webu a takhle pošmodrchané jsi to vkopíroval do IDE...
Takhle jsem to zkopíroval z webu.

Re: Pomoc s chybami v programu

Napsal: 14 lis 2019, 10:06
od pavel1tu
Asi sem hoď jen odkaz odkud jsi to zkopíroval.

Re: Pomoc s chybami v programu

Napsal: 14 lis 2019, 10:07
od KamilV
A máš odkaz? Třeba to z něj půjde vykuchat rychleji, než to přepisovat z toho kódu výše.

Re: Pomoc s chybami v programu

Napsal: 14 lis 2019, 12:05
od Spider 1970
Tady je ten odkaz na ten kod [https://petermobbs.wordpress.com/2014/1 ... ocus-rail/][/url].První jsem to našel na you tube,líbilo se mi to a tak jsem si nakoupil i věci a ted tohle . :evil: