9am to 6pm, having trouble!!

Hi, I’m having trouble with this code,
The idea is to rotate the solar panel from 9 am to 6 pm, every hour for 0.4 sec of signal and then
at 6 pm it will use 0.4*9 = 3.6 secs to rotate back in the opposite direction to the original position
and from 6 pm to the next morning 9am, it just rest ( do nothing)
and from 9 am the process starts again
My supervisor told me that, time/clock is not important and he wants it to be in C++ format
since time is not important i did it in this way
help me check the code if anything is wrong, the error says its logic error
also is the “rotating, HIGH” analogWrite or digitalWrite?
Really hope you guys can help!!!

#include<iostream>
using namespace std;
int clockk;
int Rotating = 8;
int AntiRotating = 7;
void setup()
{
  pinMode(Rotating = OUTPUT);
  pinMode(AntiRotating = OUTPUT);
}
void loop()
{
  clockk == 9;                 //9 am
  if (9 >= clockk <= 18)       //9am to 6 pm
  {
  digitalWrite(Rotating , HIGH);
  delay(400);
  digitalWrite(Rotating , LOW);
  delay(3599600);             //1 hour
  clockk++;
  }
  else                        // after 6pm
  {
    digitalWrite(AntiRotating, HIGH);
    delay(3600);
    digitalWrite(AntiRotating, LOW);
    delay(53996400);          //15 hours
  }
  clockk == 9;                //back to 9am
}

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn’t show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you’re using the Arduino Web Editor you will not have access to this useful tool but it’s still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

Nyihein:
the error says its logic error

What error?

When you encounter an error you’ll see a button on the right side of the orange bar “Copy error messages”. Click that button. Paste the error in a message here USING CODE TAGS (</> button on the toolbar).

Nyihein:
is the “rotating, HIGH” analogWrite or digitalWrite?

What are you talking about?

Nyihein:   pinMode(Rotating = OUTPUT);   pinMode(AntiRotating = OUTPUT);

You need to spend some time studying this: https://www.arduino.cc/reference/en/language/functions/digital-io/pinmode/

void loop()
{
  clockk == 9;                 //9 am
   //omitted if() ... else ...
  clockk == 9;                //back to 9am
}

Why are you testing if the time is 9? Even if I mis-read this as "=" assignment, it's still wrong. That would mean the time never changes from 9, of it it does, it's immediately changed back.

delay() is an extremely inefficient and imprecise way of waiting for a time period. I know the teacher said that it doesn't need to be accurate but it still is a good idea to avoid blatant inaccuracies. The millis() clock built into every Arduino is a much better way of measuring time.

delay() is an extremely inefficient and imprecise way of waiting for a time period. I know the teacher said that it doesn't need to be accurate but it still is a good idea to avoid blatant inaccuracies. The millis() clock built into every Arduino is a much better way of measuring time.

Since delay() and millis() both use the same clock, this statement is wrong.

There are very good reasons for not using delay(), but it is no more, or less, inaccurate than using millis().

delay() is significantly less accurate. Try this…

for(int i=0; i<=1000; i++) {
  Serial.println(i);
  delay(1);
}
Serial.println("Finished 1 second!");

How long does that take to run? A lot more than 1 second. How much more is outside the control of this snippet.

How long does that take to run? A lot more than 1 second. How much more is outside the control of this snippet.

The problem is NOT the call the delay(). The problem is that the loop is calling delay() AND doing Serial printing. You are not accounting for the fact that serial printing takes time.

Several things.

These are never used. No need to include them.

#include<iostream>
using namespace std;

These can be constants, since they are never changed.

int Rotating = 8;
int AntiRotating = 7;

Syntax on these is wrong.

  pinMode(Rotating = OUTPUT);
  pinMode(AntiRotating = OUTPUT);

This is a test, not an assignment like it should be. Plus, MorganS is right, it’s in the wrong place.

  clockk == 9;                 //9 am

Syntax on this is wrong. Plus, clock will never be < 9. Also, I think you don’t want it to do this at 6 pm. That would be one more hour before it AntiRotated.

  if (9 >= clockk <= 18)       //9am to 6 pm

You may need to make this long.

    delay(53996400);          //15 hours

Other than that, I think it should work.

Try this:

int clockk = 9;   // Start this at 9.
const int Rotating = 8;
const AntiRotating = 7;
void setup()
{
  pinMode(Rotating, OUTPUT);
  pinMode(AntiRotating, OUTPUT);
}
void loop()
{
  if (clockk < 18)       //9am to 6 pm
  {
    digitalWrite(Rotating , HIGH);
    delay(400);
    digitalWrite(Rotating , LOW);
    delay(3599600L);             //1 hour
    clockk++;
  }
  else                        // after 6pm
  {
    digitalWrite(AntiRotating, HIGH);
    delay(3600);
    digitalWrite(AntiRotating, LOW);
    delay(53996400L);          //15 hours
    clockk = 9;                 //9 am
  }
}

On two occasions I have been asked, — “Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?” In one case a member of the Upper, and in the other a member of the Lower, House put this question. I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.

Charles Babbage, Passages from the Life of a Philosopher (1864), ch. 5 “Difference Engine No. 1”

This constant does not need to be expressed as a long:

    delay(53996400);          //15 hours

However, it doesn't look like 15 hours, which would be 60L * 60 * 15 * 1000 = 54000000.

If there is a reason for this, it ought to be documented in the code.

Mr. aarg is correct. My bad.

Although it doesn’t hurt to include the L. In case there is an overloaded case of the function.
I also had to fix a syntax error. This time it has actually been compiled, although I didn’t run it.

Corrected code:

int clockk = 9;   // Start this at 9.
const int Rotating = 8;
const int AntiRotating = 7;

void setup()
{
  pinMode(Rotating, OUTPUT);
  pinMode(AntiRotating, OUTPUT);
}
void loop()
{
  if (clockk < 18)       //9am to 6 pm
  {
    digitalWrite(Rotating , HIGH);
    delay(400);
    digitalWrite(Rotating , LOW);
    delay(3599600L);             //1 hour (less the 400 ms we just delayed)
    clockk++;
  }
  else                        // after 6pm
  {
    digitalWrite(AntiRotating, HIGH);
    delay(3600);
    digitalWrite(AntiRotating, LOW);
    delay(53996400L);          //15 hours  (minus the 3600 ms we already delayed)
    clockk = 9;                 //9 am
  }
}

Jimmus:
Mr. aarg is correct. My bad.

Although it doesn’t hurt to include the L. In case there is an overloaded case of the function.

Not to be too nitpicky, but if it was overloaded, the constant 53996400 would evaluate to a long, and the function that takes a long parameter would be called.