Go Down

Topic: Project 12 (true/false not working?) (Read 1 time) previous topic - next topic

Thund3rmare

Hello,

I'm quite new to arduino and this is the first problem I've encountered.

I attached correctly the project to the breadboard (I checked 6 times). Yet the piezo is not reading anything.

I only have the red LED on and the Serial monitor shows. "The box is unlocked.
The box is locked." which is expected when you first run the code.

I have checked my code and I noticed the my "true/false" statements were not colored as it suggests in the book (I will attach a printscreen)

Also below I copy pasted my code in case people want to have a closer look.

Looking forward to answers.

Cheers!
 
Code: [Select]
#include <Servo.h>
Servo myServo;
const int piezo = A0;
const int switchPin = 2;
const int yellowLED = 3;
const int greenLED = 4;
const int redLED = 5;

int knockVal;
int switchVal;

const int quietKnock = 10;
const int loudKnock = 100;

boolean locked = false;
int numberofKnocks = 0;

void setup()
{
  myServo.attach(9);
  pinMode(yellowLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(redLED, OUTPUT);
  pinMode(switchPin, INPUT);

  Serial.begin(9600);
  digitalWrite(greenLED, HIGH);
  myServo.write(0);
  Serial.println("The box is unlocked.");
}

void loop()
{
  if (locked == false)
  {
    switchVal = digitalRead(switchPin);
    if(switchVal == HIGH);
    {
      locked = true;
      digitalWrite(greenLED, LOW);
      digitalWrite(redLED, HIGH);
      myServo.write(90);
      Serial.println("The box is locked.");
      delay(1000);     
    }
    if(locked == true)
    {
      knockVal = analogRead(piezo);
      if(numberofKnocks < 3 && knockVal > 0)
      {
        if(checkForKnock(knockVal) == true)
        {
          numberofKnocks++;
        }
        Serial.print(3-numberofKnocks);
        Serial.println(" more knocks to go");
      }
    }
  }

  boolean checkForKnock(int value)
  {
    if(value > quietKnock && value < loudKnock)
    {
      digitalWrite(yellowLED, HIGH);
      delay(50);
      digitalWrite(yellowLED, LOW);
      Serial.print("Valid knock of value ");
      Serial.print(value);
      return true;
    }
    else
    {
      Serial.print("Bad knock Value ");
      Serial.println(value);
      return false;
    }
  }
 

dannable

First of all, check your line 37.

Secondly, have you missed out a large chunk of code on purpose? It starts:

Code: [Select]
if(numberOfKnocks >= 3)
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

Thund3rmare

I have it in code i just miss clicked when I Copy-Pasted.

Line 37 is good.

any idea why "true/false" doesn't change color in the IDE?

oqibidipo

Line 37 is good.
Code: [Select]
if(switchVal == HIGH);
                     ^

Do you really want to do nothing when switchVal is HIGH?
If not, remove the semicolon.

Quote
any idea why "true/false" doesn't change color in the IDE?
It is highlighted, but in the default syntax highlight style the color is black.
Code: (lib/theme/syntax/default.xml) [Select]
    <style token="LITERAL_BOOLEAN" fg="000000" bold="false"/>

Go Up