gimbal - v3 (Editing) — Arduino Nano simulation

(EDITING COPY)

An interactive Arduino Nano circuit simulation you can run free in your browser on Velxio, by a36274595.

Components

Sketch code

#include <Servo.h>

bool isSwitchOn;
bool isLeftPressed;
bool isRightPressed;
bool isUpPressed;
bool isDownPressed;

Servo armHorizontal;
Servo armVertical;

float positionHorizontal = 0.90;
float positionVertical =    180;

float step = 1.0;



void setup() {

  // LED
  pinMode(13, OUTPUT);
  pinMode(5, INPUT_PULLUP);

 

  pinMode(A5, INPUT_PULLUP); // left
  pinMode(A2, INPUT_PULLUP); // right
  armHorizontal.attach(2);

  pinMode(A3, INPUT_PULLUP); // down
  pinMode(A4, INPUT_PULLUP); // up
  armVertical.attach(3);

  armHorizontal.write(positionHorizontal);
  armVertical.write(positionVertical);
}

//Simplified Code For Buttons
void armHorizontalMovingRight()
{
     
      positionHorizontal += step;
      if (positionHorizontal > 180) positionHorizontal = 180;

      armHorizontal.write(positionHorizontal);
      delay(10);
}

void armHorizontalMovingLeft()
{
    
    positionHorizontal -= step;
      if (positionHorizontal < 0) positionHorizontal = 0;

      armHorizontal.write(positionHorizontal);
      delay(10);
}

void armVerticalMovingDown()
{
    
    positionVertical -= step;
      if (positionVertical < 0) positionVertical = 0;

      armVertical.write(positionVertical);
      delay(10);
}

void armVerticalMovingUP()
{
    
     positionVertical += step;
      if (positionVertical > 180) positionVertical = 180;

      armVertical.write(positionVertical);
      delay(10);
}


//void loop starts here.
void loop()
{

  isSwitchOn = digitalRead(5);
  isLeftPressed = digitalRead(A5); //1 means not pressed. 0 means pressed
  isRightPressed = digitalRead(A2);
  isUpPressed = digitalRead(A4);
  isDownPressed = digitalRead(A3);



  // When Switch ON, Turn the Led ON





  digitalWrite(13, isSwitchOn);

  if (isSwitchOn) {
     // horiz to move right 
    // AND 
    // vert to move up

//when LED is on:
    // LEFT and UP
    if (!isLeftPressed) {
      armHorizontalMovingLeft();
      if (positionHorizontal == 0.0) 
      armVerticalMovingUP();
    }




    // RIGHT and DOWN
    if (!isRightPressed) {
      armHorizontalMovingRight();
       if (positionHorizontal == 180) 
      armVerticalMovingDown();
    }


    // DOWN and Right
    if (!isDownPressed) {
      armVerticalMovingUP();
      if (positionVertical == 180) 
      armHorizontalMovingRight();
    }

    // UP and LEFT
    if (!isUpPressed) {
      armVerticalMovingDown();
      if (positionVertical == 0.0) 
       armHorizontalMovingLeft();
    }
  
 
 
  } else {

  
    // LEFT
    if (!isLeftPressed) {
      armHorizontalMovingLeft();
    }




    // RIGHT
    if (!isRightPressed) {
      armHorizontalMovingRight();
    }


    // DOWN
    if (!isDownPressed) {
      armVerticalMovingDown();
    }

    // UP
    if (!isUpPressed) {
      armVerticalMovingUP();
    }
  
  
  }



}

//LED switch arm mechanizm

More projects by a36274595