Go Down

Topic: Not recieving Serial output from my sketch (Read 509 times) previous topic - next topic

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: [Select]
#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);
    }
  }
}
}

PeterH

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.
I only provide help via the forum - please do not contact me for private consultancy.

holmes4

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

Nico V

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).

holmes4

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

Mark

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.

Nico V

I can't see anything wrong with the logic (but it's almost 3AM over here so I could be wrong ;)). 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'.

ohhhh I just figured it out! *face palm*

heres the updated code.

Code: [Select]
#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);
    }
  }
}
}

PaulS

Code: [Select]
      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: [Select]
      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.

Go Up