Pages: 1 [2]   Go Down
Author Topic: Help with code that is not working correctly  (Read 1193 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Edison Member
*
Karma: 49
Posts: 1676
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The two 'if' statements with && should be inside the first 'if' statement so that they are only tested when a serial character is available.
Code:
void loop()
{
  buttonState = digitalRead(buttonPin);

  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    if (incomingByte == 'H' && buttonState == HIGH) {
      myservo.write(180);
      delay(1500);
      digitalWrite(ledPin, HIGH); 

    }
    if (incomingByte == 'L' && buttonState == LOW) {
      myservo.write(90);
      delay(1500);
      digitalWrite(ledPin, LOW);

    }
  }
}

Pete
Logged

France
Offline Offline
Edison Member
*
Karma: 38
Posts: 1012
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So you have to type a command AND use a button to move the servo? Are you sure you don't need to use || instead, so you either type a command, or use the button ?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the servo will be used to lock a deadbolt, the switch will detect when the door is closed so the deadbolt only moves when the door is closed, also i ahave already tried it with || with no luck
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 27386
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try adding some ()s, such as:

if ((incomingByte == 'H') && (buttonState == HIGH)) {
« Last Edit: October 15, 2012, 11:33:14 pm by Coding Badly » Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 210
Posts: 13039
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Apparently @swilliams1284 wanted a change of scenery.  He's moved the conversation here...
http://arduino.cc/forum/index.php/topic,127428.0.html
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 506
Posts: 19120
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm thinking it has something to do with the && operators? Any ideas?

You've found a problem with the && operator? Well done!

Meanwhile, please don't start a new thread about exactly the same code as the existing one.

Threads merged.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 506
Posts: 19120
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Would you mind telling us why you have to type a command on the serial console, and hit a button to lock/unlock this door?
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 506
Posts: 19120
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

in the code i have a servo move to 180 degrees if it detects that the button is in a HIGH state and it reads an H from the serial port. The individual pieces of the circuit work when being tested by them selves so its not hardware related as far as i can tell, I'm thinking it has something to do with the && operators? Any ideas?

Code:

void loop()
{
  buttonState = digitalRead(buttonPin);

if (Serial.available() > 0) {

incomingByte = Serial.read();

}

if (incomingByte == 'H' && buttonState == HIGH) {
  myservo.write(180);
  delay(1500);
  digitalWrite(ledPin, HIGH); 
 
}


You are testing incomingByte whether or not you just read it. Why?
Logged


Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 644
Posts: 50500
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Something I'm curious about. You seem to be able to use the Serial Monitor and Serial class to get data to the Arduino, but you don't seem to be able to use the Serial class and Serial Monitor to get data FROM the Arduino. Curious.
Logged

Pages: 1 [2]   Go Up
Jump to: