Go Down

Topic: Control servos through app trouble (Read 2107 times) previous topic - next topic

WallAppAlpha

I just tried it seems like I still have no luck could it be the code? and if it is what could it be? I'm new to Arduino so this is a process for me but thank you for the help

cattledog

Quote
I'm new to Arduino so this is a process
As mentioned earlier, take a big step backwards and write a simple program to turn an led on and off. This will simplify the issues with the phone/bluetooth , trinket and basic serial communications. Introduce the servos and the timer after the basics are solid.

WallAppAlpha

what about my code is there anything I need to correct I'm afraid I might have the wrong syntax

AWOL

Quote
I'm afraid I might have the wrong syntax
The compiler will find syntax errors for you.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

WallAppAlpha

ive tried but it still seems to not be working what would be the best way for me to set up the code

AWOL

I can absolutely assure you that the compiler is working.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

WallAppAlpha

Alright thank you it seems i have connection i just tested with a simple blink code and works fine so now i dont understand what the issue with my servo code is there inherently wrong? i believe to have the code and wiring correct but it seems as if my motors wont move.

cattledog

#22
Aug 09, 2018, 12:25 am Last Edit: Aug 09, 2018, 12:25 am by cattledog
Quote
Alright thank you it seems i have connection i just tested with a simple blink code and works fine so now i dont understand what the issue with my servo code is there inherently wrong?
Can you now write some simple test code for the trinket which runs the servos? Don't use any input from the BT module.

Can you run the basic example from the Adafruit _SoftServo library
https://github.com/adafruit/Adafruit_SoftServo/blob/master/examples/TrinketKnob/TrinketKnob.ino

Please post the "simple blink code" you have written.

WallAppAlpha

here is the basic blink code


Code: [Select]
#include <SoftwareSerial.h>

#define TXPIN 0 //Connected to TX on HM-10
#define RXPIN 1 //Connected to RX on HM-10

SoftwareSerial Serial(0,1); //RX|TX

// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(9600);
  while (!Serial);
  // initialize digital pin LED_BUILTIN as an outpu.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  if(Serial.available()>0) 
  {
    char w = Serial.read();
    if(w == 'a')
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)                // wait for a second
    if(w == 'b')
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
                    // wait for a second
  }
}

cattledog

Quote
here is the basic blink code
Excellent progress. This tells us that the basics of the phone/bluetooth/trinket connection are correct.

Get the trinket servo example running and it should be straightforward to combine the sketches.

WallAppAlpha

i combined the 2 sketches and i still seem to be running into issues the motors will not move.
Code: [Select]
#include <Adafruit_SoftServo.h> 
#include <SoftwareSerial.h>

#define SERVO1PIN 3
#define SERVO2PIN 4

#define TXPIN 0 //Connected to TX on HM-10
#define RXPIN 1 //Connected to RX on HM-10


int moveAmount = 1;  // change this value to change speed of servo
int pos = 90;
int pos2 =0;

SoftwareSerial Serial(1,0); //RX|TX
 
Adafruit_SoftServo myServo1, myServo2;

   
void setup() {
  Serial.begin(9600);
  while (!Serial);
  // Set up the interrupt that will refresh the servo for us automagically
  OCR0A = 0xAF;            // any number is OK
  TIMSK |= _BV(OCIE0A);    // Turn on the compare interrupt (below!)

  myServo1.attach(SERVO1PIN); 
  myServo2.attach(SERVO2PIN);   // Attach the servo to pin 0 on Trinket

  myServo1.write(12);
  myServo2.write(12);
}
 
void loop()
{
  if(Serial.available()>0) 
    {
    char w = Serial.read();
    if(w == 'a')
      for(pos = 0; pos <= 102; pos += 1)
      {
      myServo2.write(pos);
      delay(20);
      }
      for(pos2 = 0; pos2 <= 180; pos2 += 1)
      {
      myServo1.write(pos2);                 
      pos2 = pos2 + moveAmount;
      if (pos2 == 0 || pos2 == 180){
      moveAmount = -moveAmount;
      } 
      }
    delay(15);                              // Slot 1
    if(w == 'z') 
        for(pos = 0; pos <= 102; pos += 1)
        {
        myServo2.write(90);
        delay(20);
 
        }
    }
}
    // We'll take advantage of the built in millis() timer that goes off
    // to keep track of time, and refresh the servo every 20 milliseconds
    // The SIGNAL(TIMER0_COMPA_vect) function is the interrupt that will be
    // Called by the microcontroller every 2 milliseconds
    volatile uint8_t counter = 0;
    SIGNAL(TIMER0_COMPA_vect) {
      // this gets called every 2 milliseconds
    counter += 2;
      // every 20 milliseconds, refresh the servos!
    if (counter >= 20) {
      counter = 0;
      myServo1.refresh();
    }
}

cattledog

Quote
i combined the 2 sketches and i still seem to be running into issues the motors will not move.
Did you run the basic library sketch to confirm that the motors are powered and wired correctly?
If the library sketch as written works properly, did you confirm that it will work correctly on pins 3 and 4?

Code: [Select]
#include <Adafruit_SoftServo.h>  // SoftwareServo (works on non PWM pins)

// We demonstrate two servos!
#define SERVO1PIN 0   // Servo control line (orange) on Trinket Pin #0
#define SERVO2PIN 1   // Servo control line (orange) on Trinket Pin #1

#define POTPIN   1   // Potentiometer sweep (center) on Trinket Pin #2 (Analog 1)

Adafruit_SoftServo myServo1, myServo2;  //create TWO servo objects
   
void setup() {
  // Set up the interrupt that will refresh the servo for us automagically
  OCR0A = 0xAF;            // any number is OK
  TIMSK |= _BV(OCIE0A);    // Turn on the compare interrupt (below!)
 
  myServo1.attach(SERVO1PIN);   // Attach the servo to pin 0 on Trinket
  myServo1.write(90);           // Tell servo to go to position per quirk
  myServo2.attach(SERVO2PIN);   // Attach the servo to pin 1 on Trinket
  myServo2.write(90);           // Tell servo to go to position per quirk
  delay(15);                    // Wait 15ms for the servo to reach the position
}

void loop()  {
  int potValue;  // variable to read potentiometer
  int servoPos;  // variable to convert voltage on pot to servo position
  potValue=analogRead(POTPIN);                // Read voltage on potentiometer
  servoPos = map(potValue, 0, 1023, 0, 179);  // scale it to use it with the servo (value between 0 and 180)
  myServo1.write(servoPos);                    // tell servo to go to position
  myServo2.write(180-servoPos);                    // tell servo to go to position

  delay(15);                              // waits 15ms for the servo to reach the position
}

// We'll take advantage of the built in millis() timer that goes off
// to keep track of time, and refresh the servo every 20 milliseconds
volatile uint8_t counter = 0;
SIGNAL(TIMER0_COMPA_vect) {
  // this gets called every 2 milliseconds
  counter += 2;
  // every 20 milliseconds, refresh the servos!
  if (counter >= 20) {
    counter = 0;
    myServo1.refresh();
    myServo2.refresh();
  }
}


There are several things wrong with your "combined" sketch, but until you can confirm, by using the library sketch, that the hardware situation is correct, it not worth the effort to address the software problems. You need to see the servos move, before you get involved with the serial commands.

WallAppAlpha

oh my it seems as if i posted the wrong code and the servos are responsive but once i have it all together they dont work anymore

#include <Adafruit_SoftServo.h> 
#include <SoftwareSerial.h>

#define SERVO1PIN 3
#define SERVO2PIN 4

#define TXPIN 0 //Connected to TX on HM-10
#define RXPIN 1 //Connected to RX on HM-10


int moveAmount = 1;  // change this value to change speed of servo


SoftwareSerial Serial(1,0); //RX|TX
 
Adafruit_SoftServo myServo1, myServo2;

   
void setup() {
  Serial.begin(9600);
  while (!Serial);
  // Set up the interrupt that will refresh the servo for us automagically
  OCR0A = 0xAF;            // any number is OK
  TIMSK |= _BV(OCIE0A);    // Turn on the compare interrupt (below!)

  myServo1.attach(SERVO1PIN); 
  myServo2.attach(SERVO2PIN);   // Attach the servo to pin 0 on Trinket

  myServo1.write(12);
  myServo2.write(12);
}
 
void loop()
{
  if(Serial.available()>0) 
    {
    char w = Serial.read();
    int pos = 90;
    int pos2 =0;
    if(w == 'a')
      for(pos = 0; pos <= 102; pos += 1)
      {
      myServo2.write(pos);
      delay(20);
      }
      for(pos2 = 0; pos2 <= 180; pos2 += 1)
      {
      myServo1.write(pos2);                 
      pos2 = pos2 + moveAmount;
      if (pos2 == 0 || pos2 == 180){
      moveAmount = -moveAmount;
      } 
      }
    delay(15);                              // Slot 1
    if(w == 'z') 
        for(pos = 0; pos <= 102; pos += 1)
        {
        myServo2.write(90);
        delay(20);
 
        }
    }
}
    // We'll take advantage of the built in millis() timer that goes off
    // to keep track of time, and refresh the servo every 20 milliseconds
    // The SIGNAL(TIMER0_COMPA_vect) function is the interrupt that will be
    // Called by the microcontroller every 2 milliseconds
    volatile uint8_t counter = 0;
    SIGNAL(TIMER0_COMPA_vect) {
      // this gets called every 2 milliseconds
    counter += 2;
      // every 20 milliseconds, refresh the servos!
    if (counter >= 20) {
      counter = 0;
      myServo1.refresh();
    }
}

AWOL

Quote
oh my it seems as if i posted the wrong code
And now the right code badly?


Please remember to use code tags when posting code.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

cattledog

#29
Aug 09, 2018, 08:09 pm Last Edit: Aug 09, 2018, 08:11 pm by cattledog
You were doing well with the use of code tags. Don't stop now!


Here's some things I see:


1)You had this in the working code for basic bluetooth connection

Code: [Select]
#define TXPIN 0 //Connected to TX on HM-10
#define RXPIN 1 //Connected to RX on HM-10
SoftwareSerial Serial(0,1); //RX|TX


You have changed the Software Serial constructor in the latest code. Are you connected to the correct pins?

Code: [Select]

#define TXPIN 0 //Connected to TX on HM-10
#define RXPIN 1 //Connected to RX on HM-10
SoftwareSerial Serial(1,0); //RX|TX



2)You need to refresh both servos in the Timer ISR. Did you not see this in the example?
Code: [Select]
myServo1.refresh();
myServo2.refresh();


3)You need opening and closing braces to define the if conditional tests for the values of the received char. Without defining the block to be executed, you only execute the next line. In the simple BT test sketch you only had one line following the if() so it worked. It's a good habit to develop to always use braces to define the conditional block, even if its one line.

4) I'm not sure if the w == 'z' code makes any sense where you write the constant position in the for loop().

You can try this, but if it doesn't move the servos as you want, I'd take a step backwards and write more simple servo moves in response to the BT commands.

Code: [Select]
#include <Adafruit_SoftServo.h>
#include <SoftwareSerial.h>

#define SERVO1PIN 3
#define SERVO2PIN 4

#define TXPIN 0 //Connected to TX on HM-10
#define RXPIN 1 //Connected to RX on HM-10


int moveAmount = 1;  // change this value to change speed of servo


SoftwareSerial Serial(0,1); //RX|TX

Adafruit_SoftServo myServo1, myServo2;


void setup() {
  Serial.begin(9600);
  while (!Serial);
  // Set up the interrupt that will refresh the servo for us automagically
  OCR0A = 0xAF;            // any number is OK
  TIMSK |= _BV(OCIE0A);    // Turn on the compare interrupt (below!)

  myServo1.attach(SERVO1PIN);
  myServo2.attach(SERVO2PIN);   // Attach the servo to pin 0 on Trinket

  myServo1.write(12);
  myServo2.write(12);
}

void loop()
{
  if (Serial.available() > 0)
  {
    char w = Serial.read();
    int pos = 90;
    int pos2 = 0;
    if (w == 'a')
    { //add brace
      for (pos = 0; pos <= 102; pos += 1)
      {
        myServo2.write(pos);
        delay(20);
      }
      for (pos2 = 0; pos2 <= 180; pos2 += 1)
      {
        myServo1.write(pos2);
        pos2 = pos2 + moveAmount;
        if (pos2 == 0 || pos2 == 180) {
          moveAmount = -moveAmount;
        }
      }
      delay(15);
    }//add brace
    if (w == 'z')
    { //add brace
      for (pos = 0; pos <= 102; pos += 1)
      {
        myServo2.write(90);
        delay(20);

      }
    }//add brace
  }
}
// We'll take advantage of the built in millis() timer that goes off
// to keep track of time, and refresh the servo every 20 milliseconds
// The SIGNAL(TIMER0_COMPA_vect) function is the interrupt that will be
// Called by the microcontroller every 2 milliseconds
volatile uint8_t counter = 0;
SIGNAL(TIMER0_COMPA_vect) {
  // this gets called every 2 milliseconds
  counter += 2;
  // every 20 milliseconds, refresh the servos!
  if (counter >= 20) {
    counter = 0;
    myServo1.refresh();
    myServo2.refresh();//add this line for second servo
  }
}



Go Up