Svp-1284 set up help

I have basic setup questions:

  1. To make a motor turn on using an external micro switch , where do I plug it in on the svp-1284 ? Is it a digital or analog input?

  2. In atmel studio 6.2 what key word or command word would I use to tell the program to listen to the micro switch/its pin location…for example, if the micro switch gets activated it will turn on a motor, how do I “code in” the micro switch.

3.Does my micro switch need 2 or 3 prongs? The application would be…when the switch is activated it makes a motor start for a short time, and if the button is activated again, the motor starts again but is reversed. The switch is a snap action switch.

  1. In atmel studio 6.2 how do I loop a program… for example how do I loop the following: set m1 speed(20) ; delay ms(200) ; set m1 speed(-20); delay ms(200)

The support files did not help much. Thanks in advace!

Hello.

You can connect an external switch to a free digital IO pin on the Orangutan to trigger your motor movements. You can find out which pins are free in the “AVR Pin Assignment Table Sorted by Pin” section of the Orangutan SVP user’s guide. You might find the example program in the “Orangutan Digital I/O Functions” section of the Pololu AVR C/C++ Library User’s Guide helpful.

From the description of your application, it sounds like a normally open pushbutton switch would be ideal; however, you probably can use either 2 or 3 prong switches for your application.

There are multiple ways to loop a function in C++. For example, you might look into while() and for() loops.

If you run into problems while programming your Orangutan SVP, you could post your code here, and I would be happy to take a look at it and make suggestions.

- Jeremy

I have got it all set up and the demo program works well… Is their any way to program an ‘every other time’ program… an example of this would be : a single external switch is activated (just clicked) and a motor goes one way, the same switch is activated(clicked)again and the motor goes back the opposite way, thus every time the button is activated(clicked) the motor changes direction. Is this possible with one external 2 or even 3 prong prong micro switch? I am very new at this and need help, thanks!

As I mentioned in my previous post, you could probably use a 2 or 3 prong switch to trigger an event. The example program in the “Orangutan Digital I/O Functions” section of the Pololu AVR C/C++ Library User’s Guide should be a good starting point for your application. You will probably want to have a variable that you toggle the state of each time your button is activated to keep track of the current motor direction so you can change it.

Since you are new to programming microcontrollers, you might consider switching to a more beginner-friendly platform, like an Arduino. Arduino is an easy-to-use open-source electronics prototyping platform. There are a lot of resources available on their website, and we have an Arduino kit that helps teach the basics of programming and electronics.

- Jeremy

Jeremy, above you said you would help and suggest me with my program. What I got is not quite doing what I want, I want to press my external button and make the motor turn on for a short time and stop, And if the button is pressed again I want my motor to turn on in the other direction and stop… Right now it is doing half of what I want, If I press the button it goes from one direction to another and stays in that direction, how do I code so when I press the button again it goes back to the original direction… Here is what I got so far:

[code]#include <pololu/orangutan.h>

int main()
{
set_digital_input(IO_C1, PULL_UP_ENABLED);

while(1)
{
    if(is_digital_input_high(IO_C1)) 
    {
        (set_m1_speed(60));
    }
   
   if (is_digital_input_high(IO_C1)) 
   {break; if (is_digital_input_high(IO_C1));
   }
   
    else
    {
        (set_m1_speed(-60));
    }
}

}[/code]

Please help with any suggestions. Thanks!!!

Instead of setting your speed inside your if statement, you might try toggling the state of a variable every time the button is activated like I suggested in my previous post. Below is an example of how you can toggle a variable with a switch and set the speed and direction of the motor accordingly:

[code]
int main()
{
set_digital_input(IO_C1, PULL_UP_ENABLED);
bool forward = true;

while(1){
if(is_digital_input_high(IO_C1))
{
forard = !forward;
}

if(forward)
{
  set_m1_speed(60);
  // this delay prevents the direction from changing 
  // multiple times from a single button press
  delay_ms(500); 
}
else
{
  set_m1_speed(-60);
  delay_ms(500);
}

}[/code]

By the way, you should use the [ code ] tags to post your code directly to the forum so others can easily view it.

- Jeremy

Jeremy, it is so close, I have tested it and need help adding a delay to stop the motors, I have tried adding; delay_ms(200);set_m1_speed(0);(see in red below) behind the existing motor speed command but it omits the button command and does not work properly… where would you add that to make the motor stop after 200ms of running?

[quote]#include <pololu/orangutan.h>
#include <stdbool.h>
int main()
{
set_digital_input(IO_C1, PULL_UP_ENABLED);
bool forward = true;
while(1){
if(is_digital_input_high(IO_C1))
{
forward = !forward;
}

	if(forward)
	{
		set_m1_speed(60); <font color="#FF0000">(I have tried adding the command here... But it does not work right)</font>
		// this delay prevents the direction from changing multiple times from a single button press
		delay_ms(500);
	}
	else
	{
		set_m1_speed(-60); <font color="#FF0000">(I have tried adding the command here... But it does not work right)</font>
		// this delay prevents the direction from changing multiple times from a single button press
		delay_ms(500);
	}
} 

}[/quote]

After the program sets the speed to 0, the program will loop back to the if statement and set the speed back to 60. You can verify this by adding a large delay (couple seconds) after set_m1_speed(0); and see it rest for a some time before looping back. From your description, it sounds like you only want to drive the motor briefly once per button press. I suggest you try modifying the if statement so it does not enter it unless the button was pressed in the last cycle.

- Jeremy

I have done what it needs to do, thank you for helping me and making this work, you made a big difference, I really appreciate it !!

                                                                                                                    Thanks!

I am glad you got it working. Thanks for letting us know.

Good luck with the rest of your project. If you feel like sharing, you can post about your project in the Share Your Projects section of our forum.

- Jeremy

One more question, how would you change the following code to count. LCD starts at 0 then my external micro switch is pressed, and it displays 1 on the LCD, and if it is pressed again, it would display 2 , thus it counts,etc…

int x = 1

if(is_digital_input_high(IO_C1)); ++x ; print(x);

This just is not being accepted into my IDE, how would you do it?..Thanks.

Hello.

Your if statement won’t work because you didn’t use brackets. Also, you should be sure to define x outside of any functions, near the top of your file, and use print_long() for printing numbers. Taken together, I think you should try something like this instead:

[code]int x = 1;

if(is_digital_input_high(IO_C1))
{
x++ ;
clear();
print_long(x);
}[/code]

If you have any additional trouble, can you post the simplest version of your entire code that should work but doesn’t? Also, please give a description of what is wrong with the code, including the exact text of any error messages you see when compiling it.

-Jon

Ok I changed it a little bit, I put “int x=.5” so when I press the button a “1” comes up on the lcd… Before with “int x=1” when I pressed the button a “2” showed up. Here is what I got.

[code]#include <pololu/orangutan.h>

int main()
{
play_from_program_space(PSTR(">g32>>c32")); // Play welcoming notes.

while(1)

{
int x = .5;

if (is_digital_input_high(IO_C1))
{ x++ ;
clear();
print_long(x);
}

}

}[/code]

So when I click the button it puts a 1 on the lcd, but then It stops…If I hit the button again nothing happens, It is not looping even with the while(1).Its close but its not quite their.

Because of where you added “int x = 5;” in your code, you are re-initializing x every time your while loop executes, which means it is constantly being replaced with the value 0.5. You should instantiate x before you call your while loop. Also, x cannot store 0.5 because 0.5 is not an integer.

By the way, from the questions you are asking, it seems like you are not familiar with programming in the C language. Reading a book or tutorial about C would probably help.

-Jon

I will change my .5 to a real integer (1). But how do I instantiate x before I call my while loop?
To do for instance, I click my external micro switch and it counts each button click on the lcd.

You can move that line above your while loop to call it before calling the while loop.

-Jon