Hello,
I'm fairly knew to programming Arduinos and I'm having this problem with a project that I'm doing for the town escape room.
The project is a clock that gets powered when the lights turn off, for a set number of "ticks" then powered off and then powered back on for another set number of "ticks" to give a code for a lock. The problem is the clock wont power back on after it gets turned off the first time.
Any help would be great!
and it is for a non-profit organization.
The code can be found below.
#define ACTION 8 // pin 8 for action to power clock
int DETECT = 1;
int D = 0;
int LightLevel = 150;
int num=0;
void setup()
{
Serial.begin(9600);
pinMode(DETECT, INPUT);//define detect input pin 1
pinMode(ACTION, OUTPUT);//define ACTION output pin 8
}
using namespace std;
void loop()
{
D = analogRead(DETECT);
Serial.println(D);
Serial.println(num);
//see if light on
if(D<LightLevel)
{
num++;
if( (10>num) || (20>num>15) || (30>num>25) ) //times i want clock to be powered
{
digitalWrite(ACTION,HIGH);
}
else
{
digitalWrite(ACTION,LOW);
}
}
else
{
num=0;
}
delay(1000);
}
Comparisons need to be separated. Yoda notation is harder to read than:
if(num < 10 || (num < 20 && num > 15) || (num < 30 && num > 25))
(If I interpret what you want to do correctly).
If you turn on compiler warnings, it will show you that you have a problem:
/home/pi/Arduino/sketch_jul13a/sketch_jul13a.ino:27:34: warning: comparison of constant '15' with boolean expression is always false [-Wbool-compare]
  if ( (10 > num) || (20 > num > 15) || (30 > num > 25) ) { //times i want clock to be powered
                 ^
/home/pi/Arduino/sketch_jul13a/sketch_jul13a.ino:27:28: warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses]
  if ( (10 > num) || (20 > num > 15) || (30 > num > 25) ) { //times i want clock to be powered
              ^
/home/pi/Arduino/sketch_jul13a/sketch_jul13a.ino:27:53: warning: comparison of constant '25' with boolean expression is always false [-Wbool-compare]
  if ( (10 > num) || (20 > num > 15) || (30 > num > 25) ) { //times i want clock to be powered
                          ^
/home/pi/Arduino/sketch_jul13a/sketch_jul13a.ino:27:47: warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses]
  if ( (10 > num) || (20 > num > 15) || (30 > num > 25) ) { //times i want clock to be powered
                       ^
You will need to break it down to individual comparisons, such as:
//original code
if ( (10 > num) || (20 > num > 15) ||...... Â
//code that actually does what you intend
if ( (10 > num) || ((20 > num ) && (num > 15)) || .....
Ok, thanks! got it working 