Doing things at the same time?

Hi,

first of all, i have to say that i am really really new to arduino/microcontrollers and programming!
so here are my questions:
i have a Arduino here, with a RTC, an ethernet shield, and a stepper motor. i can somehow use all the parts seperately, but i am wondering how to put them together.
The code is the following:

#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include "RTClib.h"
#include <Stepper.h>


RTC_DS1307 RTC;

const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 53,52,51,50);            

byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,10,10);
int st,mi,se;
EthernetServer server(80);

void setup() {

  myStepper.setSpeed(60);
  Serial.begin(9600);
  Wire.begin();
  RTC.begin();

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());

}

void loop () {
  DateTime now = RTC.now();

  DateTime korrekt (now.unixtime() - 960);

  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.print("UHRZEIT ");
          client.print(korrekt.hour(), DEC);
          client.print(":");
          client.print(korrekt.minute(), DEC);
          client.print(":");
          client.print(korrekt.second(), DEC);
          client.println("
");       
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

How could i get the stepper motor to do x steps everytime “korrekt.second(), DEC” is 10?
and second: why is the RTC around 960s offset to the real time?

I hope these questions are not too basic for you guys here, and i hope someone can help me!

Thanks a lot!!

PS.: i tried something like:

 se = korrekt.second(), DEC;
    if (se = 10) {
     myStepper.step(1000);
    }

but it simply did nothing … Ideas?

What are you using to drive the stepper ?

pcace: Hi,

first of all, i have to say that i am really really new to arduino/microcontrollers and programming! so here are my questions: i have a Arduino here, with a RTC, an ethernet shield, and a stepper motor. i can somehow use all the parts seperately, but i am wondering how to put them together.

Seriously, learn the basics before attempting the complex. Learn to make hamburgers before Chateau Briand and all the trimmings. You will keep more hair that way.

Your IDE comes with examples you can open with the IDE. Click menues, File->Examples-> and go through sections 1) Basics, 2) Digital, 3) Analog and 5) Control. Section 4 teaches bad habits, the use of C++ Strings in some of the examples, sorry nothing is perfect. But go through 1,2,3 and 5 to learn what will save you a lot of time and typing right from the start.

Here is a clear, simple, informative blog that can get you started on what you want.

How to do multiple things at once ... like cook bacon and eggs

http://www.gammon.com.au/forum/?id=11411

PS.: i tried something like:
Code:

 se = korrekt.second(), DEC;

Why? What possessed you to tack the ,DEC crap on the end?

    if (se = 10)Probably the most common mistake seen here. Look up the difference between == and =

Just because the code compiles, does not mean it is free from "simple beginner faults". It is OK to make mistakes.

Just looking at the code fragment:

se = korrekt.second(), DEC;
    if (se = 10) {
     myStepper.step(1000);
    }

Line one does the following. It evaluates "korrekt.second()" meaning it calls the function. The "," in this context (outside a parameter list to a function) means after you have evaluted what is to the left, ignore it and evalute what is to the right. Use THAT as the result to pass to the "=". So here we evaluate DEC. This is just a number. Lastly we take that result and place the result in "se".

(The Arduino reference ommits a few of the less common C/C++ syntax and operators to make it easier to learn. The comma operator is one of these)

I can only guess why you put that ", DEC" there - you think that is necessary to a decimal value from the function. The DEC (or HEX, BIN ...) only have meaning as the second argument to the the print() function. All numbers inside all your variables are binary. The concept "decimal" only takes on meaning when printing (or reading from a file/keyboard) the value, that is the visual representation of the number.

The second line contains an assigment operator "=", not a comparison inside the if. The comparison you probably wanted is "==". This is a very common mistakes, happens to big experienced people too, but unfortunatly a major stumbling block while one is learning. It should be if ( se == 10 ) .

You then use "{ }" for the if-body. Good. It is not needed for a single statement, but it is good practice that makes editing of the code easier.

This was a lengthy response, as you said you are learning - the others have already said the same in shorthand (while I was typing this)

Hi,

thank you all!
i know, that i plan a too big project. so i looked for some smaller exercises:
i want to turn a stepper motor everytime when its a full minute (nonsense, but doesent matter…)

so my first plan was to just rotate the stepper back and forth with some seconds delay and here is the code:

#include <Wire.h>

#include <Stepper.h>
#include <SPI.h>

const int stepsPerRevolution = 200;
const unsigned long motorintervall = 9000;
int stepperposition;
unsigned long currenttime, looptime;
Stepper myStepper(stepsPerRevolution, 53,52,51,50);            



void setup() {

  myStepper.setSpeed(60);
  currenttime = millis ();
  looptime = currenttime;


}


void stepperrotate()

{
  if (stepperposition == 0)
  {
    myStepper.step(1000);
    stepperposition = 1000;
  }
  else
  {
    myStepper.step(-1000);
    stepperposition = 0;
  }
}

void loop ()
{
  currenttime = millis();
  if (currenttime >= (looptime + 5000)) 
  {
    stepperrotate();
    looptime = currenttime;
  }
}

what happens is, that the stepper waits a couple of seconds, than rotates back and forth without delaying… when i do something like:

void loop ()
{
    stepperrotate();
delay(1000);
}

it does exactly what i want!

What is the problem?

Thanks,

Pcace

What is the problem?

Why is the stepper connected to the SPI pins?

aahhm, its connected to "digital" pins form the Arduino mega 2560... is that wrong? i mean, it works (somehow..)

Pcace

Ok,

i can now rotate the Stepper back and forth every minute without delay!

void stepperrotate()

{
  if (stepperposition == 0)
  {
    myStepper.step(1000);
    stepperposition = 1000;
  }
  else
  {
    myStepper.step(-1000);
    stepperposition = 0;
  }
  return;
}

void loop ()
{
  DateTime now = RTC.now();

  currenttime = millis();
 /* if (currenttime >= (looptime + 10000))
  {
    Serial.println("test");
    stepperrotate();
    looptime = currenttime;
  }
*/

if ((now.second()) == 10)
{
  stepperrotate();
  Serial.print(now.second());
}
}

pcace: i mean, it works (somehow..)

I want you to understand that this is to help you. It works somehow is a problem if you want to learn to code.

The way to be able to write your own or fix or combine is to be sure about every part, what it does and how it goes and works together, and still understand that it is almost sure that you will be wrong or missing some things that you will have to learn through to work out what we call bugs in a process called debugging that usually takes way way far more time than anyone would believe it should.

So you start small and work on ways that reduce the number of things that can go wrong.

Open your IDE and click the File menu button like when you want to Open a sketch. But instead of Open, choose Examples and then 02.Digital and then BlinkWithoutDelay. Or go to that blog link I gave before and get a full clear explanation of the same thing, but at least you will know how to get to the Examples all set up for your IDE version.

Slapping libraries/code copied from elsewhere together without previous learning experience as if it's an Ikea desk and bookshelf set is not a good way to approach projects. It does YOU a disservice. Please, for your own good learn the basics first. Your confidence and understanding will increase. You can learn to 'fly on your own'.

Hi,

i totaly understand what you and the others wrote! i also read the posted links, and read (+understand) the basic codes from the arduino ide. My "somehow" was related to

Why is the stepper connected to the SPI pins?

. Maybe that was a bit confusion.

Thank you anyway for your help.

Pcace

You have the SPI library included. I don't have the shields that you do so I don't know but will SPI bus be needed for your project? You can use the SPI pins for motor but if you also use them for SPI it will not work as you wish.