Show Posts
Pages: 1 [2]
16  Using Arduino / Motors, Mechanics, and Power / Re: [HELP] Servos and comparators on: November 26, 2012, 08:51:31 am
I have modified the code, and i would like to get the last state of the sensor, in this case, when it turns 1 1 1 1 1, the uC will follow the last state it was in...

Here's the code


Code:

//Code by O.L.

#include <Servo.h>

Servo MainS;

int pos = 90;
int LM = 13;
int L = 12;
int C = 11;
int R = 10;
int RM = 9;
int position = 90;



void setup() {
  MainS.attach(8);
  pinMode(LM, INPUT);
  pinMode(L, INPUT);
  pinMode(C, INPUT);
  pinMode(R, INPUT);
  pinMode(RM, INPUT);
}

void loop()
{
 
int LMs = digitalRead(LM);
int Ls = digitalRead(L);
int Cs = digitalRead(C);
int Rs = digitalRead(R);
int RMs = digitalRead(RM);

if (LMs == 0 && Ls == 0 && Cs == 0 && Rs == 0 && RMs == 0){
   pos = 90;
        MainS.write(pos);
        position = 90;
      }
     
if (LMs == 0 && Ls == 1 && Cs == 1 && Rs == 1 && RMs == 1) {
      for(pos = position; pos <= 179; pos += 5) 
        {                               
          MainS.write(pos);             
          delay(15); 
        }
      position = 179;
      }
if (LMs == 1 && Ls == 1 && Cs == 1 && Rs == 1 && RMs == 0) {
      for(pos = position; pos >=0; pos -= 5) 
        {                               
          MainS.write(pos);             
          delay(15);
        }
      position = 0;
      }
if (LMs == 1 && Ls == 1 && Cs == 1 && Rs == 0 && RMs == 1) {
      for(pos = position; pos >=45; pos -= 5) 
        {                               
          MainS.write(pos);             
          delay(15);
        }
      position = 45;
      }
if (LMs == 1 && Ls == 0 && Cs == 1 && Rs == 1 && RMs == 1) {
      for(pos = position; pos >=135; pos -= 5) 
        {                               
          MainS.write(pos);             
          delay(15);
        }
      position = 135;
      }
if (LMs == 1 && Ls == 1 && Cs == 0 && Rs == 1 && RMs == 1) {
        pos = 90;
        MainS.write(pos);
        position = 90;
      }
if (LMs == 1 && Ls == 1 && Cs == 1 && Rs == 1 && RMs == 1) {
        pos = position;
        MainS.write(pos);
        position = 90;}
}
17  Using Arduino / Motors, Mechanics, and Power / Re: [HELP] Servos and comparators on: November 24, 2012, 08:23:06 am
Thanks for help. It is working! smiley-grin
18  Using Arduino / Motors, Mechanics, and Power / [HELP] Servos and comparators on: November 21, 2012, 10:18:57 am
Guys, I'm currently developing a code for my project, that basically turns a servo a certain number of degrees, by a corresponding pulse from the comparator circuit that i built. It is working fine, along with the reactions, but there is one problem, as there is a continuous pulse on the input of the arduino, it turns the servo, but, it goes back and forth to the desired position, making it move quite a lot, rather than staying put on the position. How to eliminate this? Here's a copy of the code.

Code:
#include <Servo.h>

Servo MainS;

int pos = 90;

int L = 12;
int C = 11;
int R = 10;


void setup() {
  MainS.attach(8);
  pinMode(L, INPUT);
  pinMode(C, INPUT);
  pinMode(R, INPUT);
}

void loop()
{

  int Ls = digitalRead(L);
  int Cs = digitalRead(C);
  int Rs = digitalRead(R);
   
  if (Ls == 1) {
      for(pos = 90; pos <= 179; pos += 5) 
        {                               
          MainS.write(pos);             
          delay(15); 
        }   
      }
     
  if (Rs == 1) {
      for(pos = 90; pos >=0; pos -= 5) 
        {                               
          MainS.write(pos);             
          delay(15);
        }   
      }
  if (Cs == 1) {
        pos = 90;
        MainS.write(pos);
      }
      delay(15);
}
19  Using Arduino / Programming Questions / Re: [HELP] Running 2 programs at once. on: July 18, 2012, 01:57:59 am
Quote
after the void setup (), then the void loop (), i put another loop ()
No, you can't have two "loop"s.

You could write "loop1" and "loop2", and call them one after the other inside "loop"

Thanks for your reply.

Okay, now about the delay part, it would follow the sequence of the code on which action would be first? As the program runs by your suggestion, let's say for example, the servo control is on loop1 with a delay of let's say, 1 hour, then the thermometer is on loop2. Would the Arduino perform the 2 tasks independently?
20  Using Arduino / Programming Questions / Re: [HELP] Running 2 programs at once. on: July 18, 2012, 12:42:39 am
Quote
Now let's say that I wanted to take temperature readings and then control a servo motor independently.
What does this mean. You have one computer and two jobs. How can you expect to do them independently? Doing one right after the other (read the temps; diddle with the servo; read the temps; diddle with the servo) makes sense. Doing the two tasks independently requires two Arduinos. Help me understand what independently means to you.

My goal is like this, for example, a person is the MCU.

He is told to do 2 things, which is first, take temperature readings, and when a certain temperature is reached, he will do an action. The second, he has to tilt a servo motor every hour. Now I want to do these things in a combined manner. What I've noticed with the arduino is that, when it has finished taking temperature readings, it moves on to the servo part of the code, and that takes an hour to finish with the delay command that is involved. Now what if the temperature was reached and it couldn't detect it since it was on the servo part of the code.

That's what i'm trying to solve.

Maybe thermometer and servo are the independent parts?

Gin, loop() is your friend. Every time through can be 100 microseconds or less later, if your code doesn't hang things up. Inside loop() you can have a block of code that watches the sensor and the next block of code runs the servo. Each runs then loop() starts again and loop() is perpetually 'now'... but only if your code doesn't block.


So, after the void setup (), then the void loop (), i put another loop () and write my code in between?
21  Using Arduino / Programming Questions / Re: [HELP] Running 2 programs at once. on: July 12, 2012, 08:03:35 pm
Let's say that I want to control 2 LEDs independently, with different pin connections and effects.

http://www.gammon.com.au/blink


Thanks for the link. I kinda understand how it works. Now let's say that I wanted to take temperature readings and then control a servo motor independently.

Using this code that I got from your link, can you please give me an idea on how to place it in this code?

Thanks.

Code:

// Which pins are connected to which LED
const byte greenLED = 12;
const byte redLED = 13;

// Time periods of blinks in milliseconds (1000 to a second).
const unsigned long greenLEDinterval = 500;
const unsigned long redLEDinterval = 1000;

// Variable holding the timer value so far. One for each "Timer"
unsigned long greenLEDtimer;
unsigned long redLEDtimer;

void setup ()
  {
  pinMode (greenLED, OUTPUT);
  pinMode (redLED, OUTPUT);
  greenLEDtimer = millis ();
  redLEDtimer = millis ();
  }  // end of setup

void toggleGreenLED ()
  {
   if (digitalRead (greenLED) == LOW)
      digitalWrite (greenLED, HIGH);
   else
      digitalWrite (greenLED, LOW);

  // remember when we toggled it
  greenLEDtimer = millis (); 
  }  // end of toggleGreenLED

void toggleRedLED ()
  {
   if (digitalRead (redLED) == LOW)
      digitalWrite (redLED, HIGH);
   else
      digitalWrite (redLED, LOW);

  // remember when we toggled it
  redLEDtimer = millis (); 
  }  // end of toggleRedLED

void loop ()
  {

  // Handling the blink of one LED.
  if ( (millis () - greenLEDtimer) >= greenLEDinterval)
     toggleGreenLED ();

  // The other LED is controlled the same way. Repeat for more LEDs
  if ( (millis () - redLEDtimer) >= redLEDinterval)
    toggleRedLED ();

/* Other code that needs to execute goes here.
   It will be called many thousand times per second because the above code
   does not wait for the LED blink interval to finish. */

}  // end of loop



22  Using Arduino / Programming Questions / Re: [HELP] Running 2 programs at once. on: July 11, 2012, 11:46:51 pm
thanks for your replies.

Let's say that I want to control 2 LEDs independently, with different pin connections and effects.

WizenedEE mentioned something about the code being in the main loop. How do I make 2 independent loops?

I've given values for the pinouts for the second LED, so where do I put it in the code for an independent operation?

I got this code from the examples library in the Arduino software, and I added the new pinout on the second line of the code.

Regarding the delay, how do I program it without affecting eachother?

Code:
int led = 13;
int led0 = 9;

void setup() {                
  pinMode(led, OUTPUT);  
  pinMode(led0, OUTPUT);
}

void loop() {
  digitalWrite(led, HIGH);  
  delay(1000);              
  digitalWrite(led, LOW);    
  delay(1000);              
}
23  Using Arduino / Programming Questions / Re: [HELP] Running 2 programs at once. on: July 11, 2012, 03:40:41 am
Will the delay command have any effect?
24  Using Arduino / Programming Questions / [HELP] Running 2 programs at once. on: July 10, 2012, 11:23:27 pm
I just got the arduino, and basically, i'm going to make a program that controls a servo independently with a set or instructions, and reads values from a temperature sensor independently and does another action when a certain value is reached.

I'm planning to do this with one arduino, is this possible?
Pages: 1 [2]