2017-06-04 152 views
0

我有一個簡單的程序:無法獲取Serial.print工作

void setup() { 
    Serial.begin(9600); 
} 

void loop() { 
    int x = 0; 
    Serial.print(x++); 
} 

每當我試圖尋找和查看序列日誌不顯示任何東西,我在做什麼錯。我正在使用Arduino Leonardo,草圖編譯和上傳。

-----------------更新1 ---------------

我真的想要得到控制檯打印出下面的代碼。然而,因爲它失敗了,我做了一個簡短而簡單的程序:

// SparkFun Soil Moisture Sensor and Serial LCD example 1 
// By Sarah Al-Mutlaq 2015 
// Sketch reads sensor and desplays level and whether soil is wet or dry 

// Use the softwareserial library to create a new "soft" serial port 
// for the display. This prevents display corruption when uploading code. 
#include <SoftwareSerial.h> 

// Attach the serial display's RX line to digital pin 2 
SoftwareSerial mySerial(3,2); // pin 2 = TX, pin 3 = RX (unused) 


// Here we are setting up some water thersholds that we will 
// use later. Note that you will need to change these to match 
// your soil type and environment. 

int thresholdUp = 400; 
int thresholdDown = 250; 

// We are setting up the pin A0 on the redboard to be our sensor 
// pin input: 

int sensorPin = A0; 


void setup(){ 
    mySerial.begin(9600); // set up serial port for 9600 baud (speed) 
    delay(500); // wait for display to boot up 
} 

void loop(){ 
    // Here we are declaring a string, which are lines of words, 
    // and we want DisplayWords to be the words displayed on 
    // the LCD screen, which will change based on whether the soil 
    // wet or dry based on our threshold values above. 
    String DisplayWords; 

    // We need to set up a pin to get the value that the soil 
    // moisture sensor is outputting, so sensorValue will get the 
    // analog value from the sensor pin A0 on the redboard that we 
    // set up earlier. 

    int sensorValue; 
    sensorValue = analogRead(sensorPin); 

    // move cursor to beginning of first line on LCD: 
    mySerial.write(254); 
    mySerial.write(128); 

    // clear display: 
    mySerial.write("    "); 
    mySerial.write("    "); 

    // move cursor to beginning of first line of the LCD screen: 
    mySerial.write(254); 
    mySerial.write(128); 

    //Write what we want to desplay on the screen: 
    mySerial.write("Water Level: "); 
    mySerial.print(sensorValue); //Using .print instead of .write for values 

    // Now we are going to check if the water level is below a 
    // out thresholdDown value we set earlier, and if it is have 
    // words "Dry, Water it!" display one column over on the first 
    // row: 

    if (sensorValue <= thresholdDown){ 
    // move cursor to beginning of second line on LCD: 
    mySerial.write(254); 
    mySerial.write(192); 

    DisplayWords = "Dry, Water it!"; 
    mySerial.print(DisplayWords); 

    // If the value is not below our thresholdDown value we want to 
    // check if it is above our thresholdUp value, and if it is 
    // change the display words to "Wet, Leave it!": 



    } else if (sensorValue >= thresholdUp){ 
    // move cursor to beginning of second line on LCD: 
    mySerial.write(254); 
    mySerial.write(192); 

    DisplayWords = "Wet, Leave it!"; 
    mySerial.print(DisplayWords); 

    // Otherwise if it is inbetween the two values we want it to 
    // the display it had, so if our soil was really wet and drying 
    // the words would only change to "Dry, Water it!" when it got to the lower threshold 
    // (thresholdDown), but if it was dry and getting wetter the words 
    // would only change to "Wet, Leave it!" when it got to the upper 
    // threshold (thresholdUp_): 

    } else { 
    // move cursor to beginning of second line on LCD: 
    mySerial.write(254); 
    mySerial.write(192); 

    mySerial.print(DisplayWords); 
    } 

    delay(500); //wait for half a second, so it is easier to read 
} 

回答

1

我現在看到你正在控制顯示器。使用這些不像發送字節到終端模擬器。他們必須受到控制。控制這些串行顯示通常包括髮送一些初始化命令,如果它們具有相同的顯示控制器,則這些初始化命令對於不同的顯示器通常(但不總是)相似。

我不知道你正在使用什麼顯示部分,但我想冒險一些初始化命令丟失。 500ms延遲通常是其中的一部分,但在發送光標定位命令之前,請檢查您的文檔是否需要發送任何其他init命令。

通常情況下,您必須發送一個初始字節命令,將顯示器置於命令模式,發送一些配置命令,然後退出命令模式以實際使用顯示器。如果是基於Hitachi 44780的顯示器,則可以在44780 datasheet上查看數據表。

+0

請參閱更新之一 – Drew1208

+0

@ Drew1208完全根據看到您的LCD控制改變了我的答案。 – TomServo