Go Down

Topic: Servo Motors Move on their own- not being activated by capacitative touch sensor (Read 1 time) previous topic - next topic

Lgms

#include <Servo.h>

// Variable Setup
int capValue[5];
int capSense[5];
int capThreshold;
int ledPins[5];
int testAngle;
int emojiToggle;
int emojiState;
int activeServos;
int servoDirection[4];
int servoAngle[4];
int armMin;
int armMax;
int analogIn;
Servo roboServo[4];

void setup() {

  // Servo Setup
  roboServo[0].attach(7); // Pin 7: fullServo
  roboServo[1].attach(3); // Pin 3: handServo
  //roboServo[2].attach(); // rotatorServo
  //roboServo[3].attach(); // shoulderServo
  for (int i=0;i<5;i++) {
    servoDirection = 1; // (-'ve is opposite direction, can be increased if code changed below...)
  }
  activeServos = 1; //(+1)

  // Emoji State (and LEDS)
  emojiToggle = 0; // 0 = unpressed, 1 = pressed
  emojiState = 0; // loops after 4...
  capThreshold = 0;
  analogIn = 1; // Mode: analog or digital
  armMin = 20;
  armMax = 160;

  // Pin references
  if (analogIn) {
    capSense[0] = A0;
    capSense[1] = A1;
    capSense[2] = A2;
    capSense[3] = A3;
    capSense[4] = A4;
  } else {
    capSense[0] = 14;
    capSense[1] = 15;
    capSense[2] = 16;
    capSense[3] = 17;
    capSense[4] = 18;
  }
  ledPins[0] = 6; //These are analog out pins (PWM)
  ledPins[1] = 9;
  ledPins[2] = 10;
  ledPins[3] = 11;
  ledPins[4] = 12;

  // Set up pin I/O
  for (int i=0;i<5;i++) {
    pinMode(capSense, INPUT);
    pinMode(ledPins, OUTPUT);
  }

  // Default Serial Value (9600)
  Serial.begin(9600);
}

void loop() {

  // Trigger LEDs with Capacitor Touch Values
  for (int i=0;i<5;i++) { // looping through all sensor inputs

    if (analogIn) { // Analog or digital read
      capValue = analogRead(capSense); // get value from capacitor sensor
      capValue = map(capValue, 0, 1023, 0, 255);
    } else {
      capValue = digitalRead(capSense);
    }
   
    if (capValue > capThreshold) { // High recieved

      // Activate Emoji Toggle
      if (i == 0) {
       
        // Should toggle-trigger a state change
        if (emojiToggle == 0) {
          emojiState++;

          Serial.println("Toggle Flag\n");
         
          if (emojiState >= 5) { // Loop Emoji State
            emojiState = 0; 
          }
        emojiToggle = 1;
        }
       
      } else if (i < activeServos) {

        Serial.println("Servo\n");

        // Flip Direction at Limits
        if ((servoAngle <= armMin && servoDirection == -1) || (servoAngle >= armMax && servoDirection == 1)) {
          servoDirection *= -1;   
         
        }
        // Increment servo angle
        servoAngle += servoDirection;

        // Apply angle to servo
        roboServo.write(servoAngle);

        // STUB: reverse direction on release?
       
      }
     
    } else { // Low received
       
      // Turn off toggle
      if (emojiToggle == 1) {
        emojiToggle = 0; 
      }
    }

    if (i == emojiState) { // Emoji Light
      // Move to target angle for select emoji
      int penta = 72;
      if (servoAngle[0] > emojiState * penta) {
        servoAngle[0]--;
      } else if (servoAngle[0] < emojiState * penta) {
        servoAngle[0]++;
      } else if (servoAngle[0] == emojiState * penta) {
        // drive LED
        if (analogIn) {
          analogWrite(ledPins[emojiState], capValue);
        } else {
          digitalWrite(ledPins[emojiState], HIGH);
        }
      }
     
    } else { // Unused LEDs
      if (analogIn) {
        analogWrite(ledPins, 0); 
      } else {
        digitalWrite(ledPins, LOW); 
      }
    }
  }

 
}

jremington

Please edit your post to add code tags, after reading "How to use this forum".

Most likely, your servo power supply is inadequate. Use a separate 5 to 6V regulated supply capable of providing 1 Ampere per servo, and don't forget to connect the grounds.

TomGeorge

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What are you using to power the servos.

Did you develop your code in stages?
Have you got code that just controls ONE servo so you know your wiring is okay?

Thanks.. Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up