Close

The FirmWare...

A project log for Sunpak PanTilt hack(Arduino Transplant)

Replacing onboard nec MCU with Arduino.

daniel-fraustoDaniel Frausto 07/12/2016 at 21:080 Comments

This FirmWare got things going...

// set pin numbers:
const int sw2 = 2;           // Down limit
const int sw3 = 3;           // Up Limit
const int ledPin =  13;      // Pwr L.E.D
const int ic1_2 = 8;         // TA8409S IN2
const int ic1_1 = 9;         // TA8409S IN1
const int enable = 10;       // TA8409S Enable (motor)

boolean sw2State = HIGH;            // Down Limit State
boolean sw3State = HIGH;           // Up   Limit State

void setup() {
  Serial.begin(9600);
  // Setting L.E.D and Motor In, Enable pins as an output:
  pinMode(ic1_1, OUTPUT);
  pinMode(ic1_2, OUTPUT);
  pinMode(enable, OUTPUT);
  pinMode(ledPin, OUTPUT);
  // Setting limit switches pins as inputs
  pinMode(sw2, INPUT_PULLUP);
  pinMode(sw3, INPUT_PULLUP);
  // Enables motor
  digitalWrite(enable, 1);
  //Starts program down
  down();
}

void loop() {
  // reads the state of the limit switches
  //Serial.print("Reading SW2 State\n");
  sw2State = digitalRead(sw2);
  //Serial.print("Reading SW3 State\n");
  sw3State = digitalRead(sw3);
  
   if(debounce_sw3(sw3State) == LOW && sw3State == HIGH)
  {
     Serial.println("Up Limit Reached\n Going Down\n");
    down();
    sw3State = LOW;
  }
   
 if(debounce_sw2(sw2State) == LOW && sw2State == HIGH)
  {
    Serial.println("Down Limit Reached\n Going Up\n");
    up();
    sw2State = LOW;
  }
 
boolean debounce_sw2(boolean state)
{
  boolean stateNow = digitalRead(sw2);
  if(state!=stateNow)
  {
    delay(10);
    stateNow = digitalRead(sw2);
  }
  return stateNow;
  
}

boolean debounce_sw3(boolean state)
{
  boolean stateNow = digitalRead(sw3);
  if(state!=stateNow)
  {
    delay(10);
    stateNow = digitalRead(sw3);
  }
  return stateNow;
  
}

void up() {
  Serial.println("Up started\n");
  digitalWrite(ic1_1, LOW);
  digitalWrite(ic1_2, HIGH);
}

void down() {
  Serial.println("Down started\n");
  digitalWrite(ic1_1, HIGH);
  digitalWrite(ic1_2, LOW);
}

void err() {
  digitalWrite(enable, LOW);
  digitalWrite(ic1_1, LOW);
  digitalWrite(ic1_2, LOW);
}

Discussions