2

Recently I got this code from a friend which is unfortunately unavailable right now.

When button is pressed two halfbridges are supposed to change directions with increasing frequency of direction change until button is pressed again and than that frequency remains same. Potentiometar is there to regulate that incriese of frequency. For some reason one output is always on other one gets one when I press the button.

This is mentioned double halfbridge board: http://www.ebay.de/itm/272620735999?_trksid=p2060353.m2749.l2649&var=571741857121&ssPageName=STRK%3AMEBIDX%3AIT

It is working, I checked button and potentiometer too,

Here is the code:

#include "options.h"

float ratio = CHANGE_FREQUENCY_MAX_SEC / 1023.0;

void setup() {
  pinMode(PIN_PWM_COIL_1, OUTPUT);
  pinMode(PIN_PWM_COIL_2, OUTPUT);
  pinMode(PIN_DIR_MOTOR, OUTPUT);
  pinMode(PIN_START_STOP, INPUT_PULLUP);

  digitalWrite(PIN_DIR_MOTOR, HIGH);
  digitalWrite(PIN_PWM_COIL_1, HIGH);
  digitalWrite(PIN_PWM_COIL_2, LOW);
}

void loop() {
  float voltage = 0;
  long steps;
  long pwm_half_period;
  unsigned long change_freq_time = 0;
  unsigned long value_steps = 0;
  unsigned int stop_freq = 0;

  if (digitalRead(PIN_START_STOP)) {
    return;    
  }

  steps = (FREQUENCY_STOP_HZ - FREQUENCY_START_HZ) / FREQUENCY_STEP_HZ;

  if (FREQUENCY_STOP_HZ < FREQUENCY_START_HZ ) {
     return; 
  }

  // If max number is reached we do nothing error in seetings 
  if (steps > FREQUENCY_MAX_CHANGES) {
    return;
  }

  value_steps =  analogRead(PIN_POTENTIONMETER_IN) * ratio + CHANGE_FREQUENCY_MIN_SEC;
  value_steps *= 1000000;

  pwm_half_period  = 500000 / FREQUENCY_START_HZ ;

  change_freq_time = 0;

  while(!digitalRead(PIN_START_STOP)) {

    digitalWrite(PIN_PWM_COIL_1, LOW);
    digitalWrite(PIN_PWM_COIL_2, HIGH);
    delayMicroseconds(pwm_half_period); 

    digitalWrite(PIN_PWM_COIL_1, HIGH);
    digitalWrite(PIN_PWM_COIL_2, LOW);
    delayMicroseconds(pwm_half_period); 

    change_freq_time++;

    if(value_steps == change_freq_time) {
      change_freq_time = 0;
      pwm_half_period += 500000 / FREQUENCY_STEP_HZ;
      stop_freq = 1000000 / FREQUENCY_STOP_HZ;

      if(FREQUENCY_STOP_HZ == (2 * pwm_half_period)) {
         break; 
      }
    }
  }
}

and here is options.h

#ifndef __OPTIONS_H__
#define __OPTIONS_H__

#define PIN_PWM_COIL_1    5
#define PIN_PWM_COIL_2    6

#define PIN_DIR_MOTOR     4

#define PIN_START_STOP    7

#define PIN_POTENTIONMETER_IN A0

#define FREQUENCY_START_HZ    5
#define FREQUENCY_STOP_HZ     1000

#define FREQUENCY_STEP_HZ     10  
#define FREQUENCY_MAX_CHANGES 120

#define CHANGE_FREQUENCY_MIN_SEC  1.0
#define CHANGE_FREQUENCY_MAX_SEC  30.0

#endif

and here is the circuit:

the circuit

  • 1
    It makes little sense to drive both PWM pins but only one DIR pin. Could you post your schematic please? – Edgar Bonet May 22 '17 at 19:42
  • There is not that much to a curcuit, Just poti and push button. _PWM_COIL_1 is on 5 _PWM_COIL_2 is on 6 and DIR_motor 2 is on 4 (this one I do not get at all) – Igor Stojanovic May 23 '17 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.