Go Down

Topic: servo control (Read 356 times) previous topic - next topic

arduino?????

I am wanting to get my servo to rotate in a loop of 180 degrees in one direction then return to the starting position 5 times how can i Incorporate that into my code?

thanks in advanced for any help.


//BEGIN CODE
#include <SoftwareSerial.h>
#define servoPin 4 // control pin for servo motor (White or yellow wire of servo)
#define minPulse 700 // minimum servo position (Open position)
#define maxPulse 2500 // maximum servo position (Closed position)
#define rxPin 8 // SOUT pin of RFID module
#define txPin 9
#define enable 2 // /ENABLE pin of RFID module
#define LED1 13 // LED output pin
#define LED2 12 // other LED output pin for two-way LED (yellow)
#define switchPin 7

boolean open = true; // default start up is to assume the lock is open
int val = 0;
char code[10];
int bytesread = 0;
int pulse, switchVal;

char tag1[11] = "0800E15D4C"; // this is size 11 because it is a NULL terminating string
char tag2[11] = "0000000000"; // 25007E0E61 for small round
char tag3[11] = "0000000000"; //  2500603A78 for card tag



void LEDControl(int state){

switch (state){
case 1:
digitalWrite(LED1,HIGH);
digitalWrite(LED2,LOW);
break;
case 2:
digitalWrite(LED2,HIGH);
digitalWrite(LED1,LOW);
break;
case 3:
for(int y=0;y<5;y++){
digitalWrite(LED1,HIGH);
digitalWrite(LED2,LOW);
delay(250);
digitalWrite(LED2,HIGH);
digitalWrite(LED1,LOW);
delay(250);
}
}

}

boolean checkTag(char *tag){

for (int x=0;x<10;x++){
if( tag
  • != code
  • ){
    return false;
    }
    }
    return true;
    }

    boolean findGoodTag(){
    if (checkTag(tag1)){ return true;}
    else if (checkTag(tag2)){ return true;}
    else if (checkTag(tag3)){ return true;}

    // Add more lines right here like the one above if you have more tags

    else{
    Serial.print("Bad tag: ");
    Serial.println(code);
    LEDControl(3);
    return false;
    }

    }
    void moveServo(){

    if (open){
    pulse = minPulse;
    open = false;
    LEDControl(1);
    }
    else if (!open){
    pulse = maxPulse;
    open = true;
    LEDControl(2);
    }

    for (int x =1;x<150;x++){
    delay (10); // don't know why this works, but it does
    digitalWrite(servoPin, HIGH); // start the pulse
    delayMicroseconds(pulse); // pulse width
    digitalWrite(servoPin, LOW); // stop the pulse
    }

    }



    void setup() {

    pinMode(servoPin, OUTPUT); // Set servo pin as an output pin
    pinMode(LED1,OUTPUT); // Set LED pin as output
    pinMode(LED2,OUTPUT); // Set LED pin as output
    Serial.begin(9600);
    Serial.println("Begin");
    pinMode(enable,OUTPUT); // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
    digitalWrite(enable, LOW); // Activate the RFID reader
    pinMode(switchPin, INPUT);
    }

    void loop() {
    SoftwareSerial RFID = SoftwareSerial(rxPin,txPin);
    RFID.begin(2400);

    switchVal = digitalRead(switchPin);



    if((val = RFID.read()) == 10)
    { // check for header
    if(switchVal == HIGH){
    Serial.println("Button");
    moveServo();
    }
    bytesread = 0;
    while(bytesread<10)
    { // read 10 digit code
    val = RFID.read();
    if((val == 10)||(val == 13))
    { // if header or stop bytes before the 10 digit reading
    break; // stop reading
    }
    code[bytesread] = val; // add the digit
    bytesread++; // ready to read next digit
    }



    if((bytesread == 10) && (findGoodTag()))
    { // if 10 digit read is complete
    digitalWrite(enable, HIGH); // dectivate the RFID reader
    moveServo();
    delay(500);
    digitalWrite(enable, LOW); // Activate the RFID reader
    }
    }
    }




Groove

#1
May 11, 2010, 02:07 pm Last Edit: May 11, 2010, 02:08 pm by GrooveFlotilla Reason: 1
Why don't you strip out all the stuff to do with RFID and concentrate on the servo, then reintegrate that back in?

Not all servos can manage 180 degrees - is that important?

When posting code, please use the # button on the editor's toolbar.
Per Arduino ad Astra

PaulS

While you're at it, this, in loop, is not a good idea:
Code: [Select]
SoftwareSerial RFID = SoftwareSerial(rxPin,txPin);
RFID.begin(2400);


There is no reason to create a new instance of SoftwareSerial every pass through loop. You should be using NewSoftSerial, anyway.

tehjrow

Quote
Why don't you strip out all the stuff to do with RFID and concentrate on the servo, then reintegrate that back in?


Start with the servo first, get it working.  It will make it way less complicated.  

Go Up