Go Down

Topic: The SD/TMRpcm disables the servo. (Read 691 times) previous topic - next topic

SanderAkaTheGod

Apr 07, 2016, 07:20 pm Last Edit: Apr 07, 2016, 07:29 pm by SanderAkaTheGod
Hey...
My servo gets disabled by the TMRpcm
when I uncheck SD.Begin the servo works fine. But if its active the servo wont work. Any help? Thanks all.

I use Arduino UNO R3 Dev.

Code: [Select]

void loop(){
   SD.begin(SD_ChipSelectPin);
}



Rest of the code:
Code: [Select]
#include <Servo.h>
#include <Keypad.h>
#include <SD.h>
#define SD_ChipSelectPin 10
#include <TMRpcm.h> 

TMRpcm som;   

Servo servolock;

const char* password = "2403";
const char* sleepPassword = "8624";
const char* pwrOff = "24032001";
int position = 0;
int switchcount = 0;
int buttonTap = 0;
int freezed = 0;
int moreFreezed = 0;
int positionReset = 0;
int sleepPosition = 0;
int pwrOffPosition = 0;
int wakePin = 2;
int servo = 7;

const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins[rows] = {1, 6, 5, 3};
byte colPins[cols] = {2, 0, 4};

Keypad keypad = Keypad(makeKeymap(keys),
                       rowPins, colPins,
                       rows, cols);

int redPin = 8;
int greenPin = A0;

boolean sleepEnable = false;
boolean unlocked = false;
boolean locked = true;
boolean notfreezed = true;
boolean sleepNow = false;

void setup() {
 som.speakerPin = 9; //11 on Mega, 9 on Uno, Nano, etc

 SD.begin(SD_ChipSelectPin);

  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  //pinMode(A1, OUTPUT);
 // pinMode(A2, OUTPUT);
 // pinMode(A3, OUTPUT);
 som.volume(7);   
 som.play("lock.wav");
 servolock.attach(servo);
}


void loop() {
 

  char key = keypad.getKey();
  //UNLOCKED - UNLOCKED - UNLOCKED - UNLOCKED - UNLOCKED
  if (unlocked == true) {
    freezed = 0;
    moreFreezed = -1;
    buttonTap = 0;
    digitalWrite(greenPin, HIGH);
    digitalWrite(redPin, LOW);
    digitalWrite(A1, LOW);
    digitalWrite(A2, LOW);
    digitalWrite(A3, LOW);

    if (key == '*' || key == '#') {
      position = 0;
      som.play("lock.wav");
      locked = true;
      notfreezed = true;
      unlocked = false;
      servolock.write(0);
    }

    if (key == '1') {
      Serial.println("1");
      switchcount++;
      digitalWrite(greenPin, LOW);
      delay(50);
      digitalWrite(greenPin, HIGH);
      som.play("test1.wav");
    }
     if (key == '2') {
      digitalWrite(greenPin, LOW);
      delay(50);
      digitalWrite(greenPin, HIGH);
      som.play("faded.wav");
    }
     if (key == '3') {
      digitalWrite(greenPin, LOW);
      delay(50);
      digitalWrite(greenPin, HIGH);
      digitalWrite(som.speakerPin, LOW);
      som.play("faded2.wav");
    }
    if (key == '0') {
      som.pause();
    }

  }
  // END - END - END - END - END - END - END - END - END

  //LOCKED - LOCKED - LOCKED - LOCKED - LOCKED - LOCKED
  if (notfreezed == true) {
   

    if (key == '*' || key == '#') {
      buttonTap = buttonTap - 1;
      digitalWrite(greenPin, HIGH);
      delay(100);
      digitalWrite(greenPin, LOW);
    }
    if (buttonTap < 0) {
      buttonTap = 0;
    }

    if (key == password[position]) {
      position++;
    }

    if (position == 4) {
      servolock.write(180);
      som.play("startup.wav");
      sleepEnable = true;
      unlocked = true;
      locked = false;
    }

    if (locked == true) {
      digitalWrite(redPin, HIGH);
      digitalWrite(greenPin, LOW);

      if (key == '1' || key == '2' || key == '3' || key == '4' || key == '5' || key == '6' || key == '7' || key == '8' || key == '9' || key == '0') {
        som.play("beep.wav");
        digitalWrite(redPin, LOW);
        delay(100);
        digitalWrite(redPin, HIGH);
        buttonTap++;
      }
      if (buttonTap == 4) {
        som.play("wrong.wav");
        position = 0;
        freezed++;
        buttonTap = 0;
        digitalWrite(redPin, HIGH);
        delay(100);
        digitalWrite(redPin, LOW);
        delay(100);
        digitalWrite(redPin, HIGH);
        delay(100);
        digitalWrite(redPin, LOW);
        delay(100);
        digitalWrite(redPin, HIGH);
      }

    }
  }
  // END - END - END - END - END - END - END - END - END
  delay(25);
  if (freezed == 1) {
    digitalWrite(A1, HIGH);
  }

  if (freezed == 2) {
    digitalWrite(A2, HIGH);
  }

  if (freezed == 3) {
    digitalWrite(A3, HIGH);
    moreFreezed++;
  }

  if (moreFreezed == 1) {
    notfreezed = false;
    digitalWrite(greenPin, HIGH);
    delay(7000);
    digitalWrite(greenPin, LOW);
    digitalWrite(redPin, LOW);
    digitalWrite(A1, LOW);
    digitalWrite(A2, LOW);
    digitalWrite(A3, LOW);
    delay(500);
    digitalWrite(greenPin, HIGH);
    digitalWrite(redPin, HIGH);
    digitalWrite(A1, HIGH);
    digitalWrite(A2, HIGH);
    digitalWrite(A3, HIGH);
    delay(500);
    digitalWrite(greenPin, LOW);
    digitalWrite(redPin, LOW);
    digitalWrite(A1, LOW);
    digitalWrite(A2, LOW);
    digitalWrite(A3, LOW);
    delay(500);
    digitalWrite(greenPin, HIGH);
    digitalWrite(redPin, HIGH);
    digitalWrite(A1, HIGH);
    digitalWrite(A2, HIGH);
    digitalWrite(A3, HIGH);
    delay(500);
    digitalWrite(greenPin, LOW);
    digitalWrite(redPin, LOW);
    digitalWrite(A1, LOW);
    digitalWrite(A2, LOW);
    digitalWrite(A3, LOW);
    delay(500);
    digitalWrite(greenPin, HIGH);
    digitalWrite(redPin, HIGH);
    digitalWrite(A1, HIGH);
    digitalWrite(A2, HIGH);
    digitalWrite(A3, HIGH);
    delay(500);
    digitalWrite(greenPin, LOW);
    digitalWrite(A1, LOW);
    digitalWrite(A2, LOW);
    digitalWrite(A3, LOW);
    moreFreezed = 2;
    freezed = 0;
    notfreezed = true;
  }
  if (moreFreezed == 2) {

  }

  if (moreFreezed == 3) {
    notfreezed = false;
    digitalWrite(greenPin, HIGH);
    delay(27000);
    digitalWrite(greenPin, LOW);
    digitalWrite(redPin, LOW);
    digitalWrite(A1, LOW);
    digitalWrite(A2, LOW);
    digitalWrite(A3, LOW);
    delay(500);
    digitalWrite(greenPin, HIGH);
    digitalWrite(redPin, HIGH);
    digitalWrite(A1, HIGH);
    digitalWrite(A2, HIGH);
    digitalWrite(A3, HIGH);
    delay(500);
    digitalWrite(greenPin, LOW);
    digitalWrite(redPin, LOW);
    digitalWrite(A1, LOW);
    digitalWrite(A2, LOW);
    digitalWrite(A3, LOW);
    delay(500);
    digitalWrite(greenPin, HIGH);
    digitalWrite(redPin, HIGH);
    digitalWrite(A1, HIGH);
    digitalWrite(A2, HIGH);
    digitalWrite(A3, HIGH);
    delay(500);
    digitalWrite(greenPin, LOW);
    digitalWrite(redPin, LOW);
    digitalWrite(A1, LOW);
    digitalWrite(A2, LOW);
    digitalWrite(A3, LOW);
    delay(500);
    digitalWrite(greenPin, HIGH);
    digitalWrite(redPin, HIGH);
    digitalWrite(A1, HIGH);
    digitalWrite(A2, HIGH);
    digitalWrite(A3, HIGH);
    delay(500);
    digitalWrite(greenPin, LOW);
    digitalWrite(A1, LOW);
    digitalWrite(A2, LOW);
    digitalWrite(A3, LOW);
    moreFreezed = 4;
    freezed = 0;
    notfreezed = true;
  }
  if (moreFreezed == 4) {

  }

  if (moreFreezed == 5) {
    notfreezed = false;
    digitalWrite(greenPin, HIGH);
    delay(57000);
    digitalWrite(greenPin, LOW);
    digitalWrite(redPin, LOW);
    digitalWrite(A1, LOW);
    digitalWrite(A2, LOW);
    digitalWrite(A3, LOW);
    delay(500);
    digitalWrite(greenPin, HIGH);
    digitalWrite(redPin, HIGH);
    digitalWrite(A1, HIGH);
    digitalWrite(A2, HIGH);
    digitalWrite(A3, HIGH);
    delay(500);
    digitalWrite(greenPin, LOW);
    digitalWrite(redPin, LOW);
    digitalWrite(A1, LOW);
    digitalWrite(A2, LOW);
    digitalWrite(A3, LOW);
    delay(500);
    digitalWrite(greenPin, HIGH);
    digitalWrite(redPin, HIGH);
    digitalWrite(A1, HIGH);
    digitalWrite(A2, HIGH);
    digitalWrite(A3, HIGH);
    delay(500);
    digitalWrite(greenPin, LOW);
    digitalWrite(redPin, LOW);
    digitalWrite(A1, LOW);
    digitalWrite(A2, LOW);
    digitalWrite(A3, LOW);
    delay(500);
    digitalWrite(greenPin, HIGH);
    digitalWrite(redPin, HIGH);
    digitalWrite(A1, HIGH);
    digitalWrite(A2, HIGH);
    digitalWrite(A3, HIGH);
    delay(500);
    digitalWrite(greenPin, LOW);
    digitalWrite(A1, LOW);
    digitalWrite(A2, LOW);
    digitalWrite(A3, LOW);
    freezed = 0;
    notfreezed = true;
    moreFreezed = 4;
  }

}

Coding Badly


Do not cross-post.  Other thread removed.


Grumpy_Mike

Yes it will they both want to use the same timer. The solution is to use an external chip to generate the servo signal or an external sound generator.

SanderAkaTheGod

#3
Apr 08, 2016, 03:21 pm Last Edit: Apr 08, 2016, 03:58 pm by SanderAkaTheGod
Cool.
So you mean I need a "SD Card" to run code on the same board?
So how do I make the SD Card run the sound code?
Thank you very much!

Yes it will they both want to use the same timer. The solution is to use an external chip to generate the servo signal or an external sound generator.

Grumpy_Mike

So you mean I need a "SD Card" to run code on the same board?
So how do I make the SD Card run the sound code?
No I don't, I am saying that because both libraries want the same hardware resource, one of the functions, either sound output or servo control has to be done with external hardware.

Go Up