Interesting A-level project

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
fin3rz
 
Posts: 14
Joined: Thu Jul 26, 2012 8:10 pm

Interesting A-level project

Post by fin3rz »

Hi guys,

I am completely new to Arduinos, in fact I received my kit earlier today! Although I'm not asking for help at the moment (might need some later!), I would like your opinions on what I'm planning.

To summarize the electronic side of the brief, I need a make a contraption that will shake an aerosol can for 2 mins, this 2 mins also needs to be able to be adjusted.

The plan is to copy the internal workings of a printer, ie a motor attached to a belt that will drive a movable clamp along a bearing. I am also probably going to have a safety guard that will automatically lower down, hooked up to a microswitch to check. These two motors will be controlled using the adafruit motor shield. I will also have a LCD 16x2 display just to be cool!

The problem at the moment is deciding how to time the adjustable 2 mins. At the moment the 'while' function seems good but the 'for' function is a possibility too, but I am unsure as to how it can be adjusted. Can anyone help?

Thanks for your time, I'll keep you all posted on how I get on.

Fin

HooctAwnFonix
 
Posts: 77
Joined: Tue Sep 13, 2011 11:47 am

Re: Interesting A-level project

Post by HooctAwnFonix »

A for loop is just a special case while loop.

Look at this:

Code: Select all

int i=0;
while(i<12) {
   Serial.println(i);
   i++
}
This is exactly the same as:

Code: Select all

for ( int i=0; i<12; i++){
   Serial.println(i);
}
The thing about using one of those loops is that you won't be able to tend to other things while you are waiting for 2 minutes. Might not be a problem for your application, but I just wanted to call your attention to that.

Anyway, if you want your Arduino to count out minutes, it won't be super precise but you will be able to figure something out. I haven't used this before, but I a quick google search turned this up which may be useful:
http://arduino.cc/playground/Code/Time

fin3rz
 
Posts: 14
Joined: Thu Jul 26, 2012 8:10 pm

Re: Interesting A-level project

Post by fin3rz »

Thanks!

I'll probably convert everything to seconds and make 4 movements (backwards then forwards x2) equal to a second, to time it reasonably accurately.

Would it be possible to set up an int, say called shakingperiod and then in the for statement have

(int i = shakingperiod; i < 12; i++)

?

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Interesting A-level project

Post by adafruit_support_mike »

The Arduino's main function is loop(), which does exactly that. As soon as it completes, it goes back and does everything again.

There's also a function that returns the amount of time the Arduino has been turned on.

Your best bet is to arrange things so your loop records a start time when told to, compares the start time to the current time every time loop() begins, then execute a short-duration 'shake' operation if necessary. The time you spend between glances at the system clock will limit the absolute accuracy with which you hit two minutes, but you probably don't need high precision for the application you described.

fin3rz
 
Posts: 14
Joined: Thu Jul 26, 2012 8:10 pm

Re: Interesting A-level project

Post by fin3rz »

Hi guys,

I have gone against your advice, just for the time being and used a while statement. In this instance I'm using LED's whilst I wait for my motor shield to arrive. I have managed to make it so that two LEDs alternate their blinking for a set time, in this case 2 seconds. I've added two extra tact switches, one to increase the time and the other to decrease. However, when I press the increase time switch, the LED's seem to blink for an infinite period and the decrease switch seems to have no effect.

I've included the code below, can anyone spot where I've gone wrong?

Code: Select all

//Sketch to flash two LEDs and off for a set period of time

int led1Pin = 13; //LED number 1
int led2Pin = 12; //LED number 2
int switchPin = 8; //Switch to start LEDs blinking
int incPin = 7; //Switch to increase time period
int decPin = 6; //Switch to decrease time period
int time; //An integer to define the time that the LEDs blink
int val;
int val2;
int val3;

void setup()
{

pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(switchPin, INPUT);
pinMode(incPin, INPUT);
pinMode(decPin, INPUT);
time = 2; //Set the inital time to two seconds

}

void loop()

{

val = digitalRead (switchPin);
val2 = digitalRead (incPin);
val3 = digitalRead (decPin);

if(val2 == HIGH)
{time = time + 1;} //Should add an extra second to the time period. 

if(val3 == HIGH) //Should subtract a second from the time period.
{time = time - 1;}

if(val == HIGH) { //If the starter switch is pressed then the below statement is carried out
while(time > 0) //While the time is above zero it does the below statement.
{
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, LOW);
delay(500);
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, HIGH);
delay(500);
time = time - 1; //Subtracts a second each time
}
digitalWrite(led1Pin, LOW); // Turns the LEDs off
digitalWrite(led2Pin, LOW);
time = 2; //Sets time back to intial value

}


}

User avatar
adafruit_support_bill
 
Posts: 88141
Joined: Sat Feb 07, 2009 10:11 am

Re: Interesting A-level project

Post by adafruit_support_bill »

In your test of the switch, you set the time to 2. So the next time around, time will always be > 0.

Code: Select all

time = 2; //Sets time back to intial value

fin3rz
 
Posts: 14
Joined: Thu Jul 26, 2012 8:10 pm

Re: Interesting A-level project

Post by fin3rz »

Not sure if I follow you here, but 'val' will no longer be equal to HIGH when the starter switch is not pressed so the statement wont be executed?

EDIT: MANAGED TO FIX THE PROBLEM. I changed the switch statements so that:

Code: Select all

if(val2 == HIGH) //Should add an extra second to the time period. 
{time = time + 1;
delay(1000);
val2 = LOW;
} 
I don't know if the switches were bouncing or what, but it seems to work.

fin3rz
 
Posts: 14
Joined: Thu Jul 26, 2012 8:10 pm

Re: Interesting A-level project

Post by fin3rz »

Due to the motor I will be using will switch directions extremely quickly, I've been informed that this could cause damage to the adafruit motor shield due to spikes of current. Firstly is this true? Secondly, I've been told that 'two opposite flyback diodes in parallel at the motor's terminals' could potentially solve this problem, is the also the case?

If so, could anyone suggest any diodes suitable for the job, I'm not sure as to what motors I will be using, but they are most likely to be 12V if this makes any difference to the diodes being selected.

I apologize if this seems question after question, but being relatively new to electronics and Arduinos, we all have to start somewhere!

User avatar
adafruit_support_bill
 
Posts: 88141
Joined: Sat Feb 07, 2009 10:11 am

Re: Interesting A-level project

Post by adafruit_support_bill »

The L293D HBridge chips have integral flyback diodes. These should be sufficient for most motors in the 0.6A range.

fin3rz
 
Posts: 14
Joined: Thu Jul 26, 2012 8:10 pm

Re: Interesting A-level project

Post by fin3rz »

Hi,

I've been experimenting with my motor shield and I have a few questions about power.

Can the arduino and motor shield be powered by USB?

The reason I ask is because I was using one of the example sketches and found that the motor would run fine on USB power but when I used 6V DC to the arduino DC in, the motor would run in one direction and omit the running in the other direction. This is using the MotorTest example.

User avatar
adafruit_support_bill
 
Posts: 88141
Joined: Sat Feb 07, 2009 10:11 am

Re: Interesting A-level project

Post by adafruit_support_bill »

Can the arduino and motor shield be powered by USB?
For small motors it will probably work, but it is not recommended.
but when I used 6V DC to the arduino DC in, ...
The Arduino needs a minimum of 7v on the DC in for proper voltage regulation.

fin3rz
 
Posts: 14
Joined: Thu Jul 26, 2012 8:10 pm

Re: Interesting A-level project

Post by fin3rz »

Thanks, I'll find a more suitable power supply!

fin3rz
 
Posts: 14
Joined: Thu Jul 26, 2012 8:10 pm

Re: Interesting A-level project

Post by fin3rz »

Another question sorry!

I'm going to have a guard that automatically closes and opens at the beginning and end of the shaking process. My friends and I were joking about possibly playing the Star Wars or the 2001 Space Odyssey theme tunes, as the guard is open, just for a laugh :D .

Whilst I have found the code for the Star Wars tune, is it possible to play this at the same time a motor is running. I think this is possible using the adafruit wave shield, however I am already using the motor shield so I won't be able to use that also. Is there any code that will do it?

User avatar
adafruit_support_bill
 
Posts: 88141
Joined: Sat Feb 07, 2009 10:11 am

Re: Interesting A-level project

Post by adafruit_support_bill »

is it possible to play this at the same time a motor is running.
What kind of motors are you using? With DC motors, it is possible to do other processing while the motors are running. However stepper motor commands are synchronous.
Is there any code that will do it?
There is the Tone function in the Arduino library. http://www.arduino.cc/en/Reference/Tone Not exactly hi-fi, but you can play tunes with it.

fin3rz
 
Posts: 14
Joined: Thu Jul 26, 2012 8:10 pm

Re: Interesting A-level project

Post by fin3rz »

Yes I will be using a DC motor on the motor shield. I am using the Tone function currently.

I presume I type "motor.run(FORWARD);", then play use the tone functions? I was under the impression that the motor couldn't run in the background and that no other processes could be preformed, but if I understand you correctly then this isn't the case.

Thanks again!!

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”