Pages: [1]   Go Down
Author Topic: Not recieving Serial output from my sketch  (Read 417 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I wrote a sketch that requires that one switch be hihg for it to function and then another switch must be high OR the arduino must seral.read 'T'. aand when that all is true then it writes a servo position and changes my led color and prints out x in the serial port.

The problem here is that nothing is working serial wise. the buttons all work, so when the loop is togggled i should see some output on the serial port but do not. I also can not make it toggle via sending it 'T'. help me please!


Code:
#include <Servo.h>

Servo myservo;
const int buttonPin = 2;
const int buttonDoor = 1;
const int ledRED = 13; // the pin that the LED is attached to
const int ledGREEN =  12; //green led
int servopos = 180;


void setup() {
   Serial.begin(9600); // serial begin
  myservo.attach(5); // attach servo to pin 5
  // initialize the LED pin as an output:
  pinMode(ledRED, OUTPUT); // led as ouput
  pinMode(ledGREEN, OUTPUT); //led as output
  pinMode(buttonPin, INPUT); // nitialize pushbuttonpin as input
  myservo.write(servopos);
 
}

void loop() {
 if(digitalRead (buttonDoor) == HIGH || Serial.read() == 'T'){
   if(digitalRead (buttonPin) == HIGH) {
    if(servopos == 180) {
       digitalWrite(ledGREEN, LOW);
       digitalWrite(ledRED, HIGH);
       Serial.println("door is locked");
       myservo.write(90);
       servopos = 90;
       delay(300);
    }
    else {
      digitalWrite(ledRED, LOW);
      digitalWrite(ledGREEN, HIGH);
      Serial.println("door is unlocked");
       myservo.write(180);
       servopos = 180;
       delay(300);
    }
  }
}
}
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The serial port uses pins 0 and 1, but you're already using them for something else. Stop using those pins and I suspect you'll find the serial port starts working properly again.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2310
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Given the delay()'s in your program you need to keep your finger on the button for at least 1 sec. Maybe you should look at blink without delay!

Mark
Logged

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

I'm with PeterH. You're using pin 1 for buttonDoor. You shouldn't be using that pin for anything but serial stuff, unless you have a board which doesn't have TX on pin 1 (eg Leonardo).
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2310
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

nicov has a very good point stay away from pin used by anything else.

Mark
Logged

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

ok I moved my pins and that got the outputting the serial to the computer working. BUT sending it "T" over serial still doesn't toggle the servo.
Logged

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

I can't see anything wrong with the logic (but it's almost 3AM over here so I could be wrong smiley-wink). If the button works, so should reading T from the serial.
Are you sure you're sending it an uppercase 'T' and not a lowercase 't'?
If that's not it you might want to add some debug output to see if you're actually receiving that 'T'.
Logged

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

ohhhh I just figured it out! *face palm*

heres the updated code.

Code:
#include <Servo.h>

Servo myservo;
const int buttonPin = 3;
const int buttonDoor = 2;
const int ledRED = 13; // the pin that the LED is attached to
const int ledGREEN =  12; //green led
int servopos = 180;


void setup() {
   Serial.begin(9600);
  myservo.attach(5); // attach servo to pin 5
  // initialize the LED pin as an output:
  pinMode(ledRED, OUTPUT); // led as ouput
  pinMode(ledGREEN, OUTPUT); //led as output
  pinMode(buttonPin, INPUT); // nitialize pushbuttonpin as input
  myservo.write(servopos);
 
}

void loop() {
 if(digitalRead (buttonDoor) == HIGH){
   if(digitalRead (buttonPin) == HIGH || Serial.read() == 'T') {
    if(servopos == 180) {
       digitalWrite(ledGREEN, LOW);
       digitalWrite(ledRED, HIGH);
       Serial.println("door is locked");
       myservo.write(90);
       servopos = 90;
       delay(300);
    }
    else {
      digitalWrite(ledRED, LOW);
      digitalWrite(ledGREEN, HIGH);
      Serial.println("door is unlocked");
       myservo.write(180);
       servopos = 180;
       delay(300);
    }
  }
}
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
      myservo.write(90);
       servopos = 90;

       myservo.write(180);
       servopos = 180;
Sooner or later, you're going to screw up and only change one of these pairs of values. I can almost guarantee it.

Code:
      servopos = 90;
       myservo.write(servopos);

       servopos = 180;
       myservo.write(servopos);
No possibility of a screw up, and it's exactly the same number of lines of code, and makes no difference to the servo.
Logged

Pages: [1]   Go Up
Jump to: