Plugging in "digitalRead" to analog codes when using Digital Pins

Is it “syntaxly” correct if I plug in “digitalRead” wherever I see “analogRead” in a code meant to read from an analog pin? I’m trying to make my TFT LCD read from a digital pin, but I could find was a code that helped it read from an analog pin (http://arduino.cc/en/Tutorial/TFTDisplayText).

#include <TFT.h>  // Arduino LCD library
#include <SPI.h>

// pin definition for the Uno
#define cs   10
#define dc   9
#define rst  8

// pin definition for the Leonardo
// #define cs   7
// #define dc   0
// #define rst  1

// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);

// char array to print to the screen
char sensorPrintout[4];

void setup() {

  // Put this line at the beginning of every sketch that uses the GLCD:
  TFTscreen.begin();

  // clear the screen with a black background
  TFTscreen.background(0, 0, 0);

  // write the static text to the screen
  // set the font color to white
  TFTscreen.stroke(255, 255, 255);
  // set the font size
  TFTscreen.setTextSize(2);
  // write the text to the top left corner of the screen
  TFTscreen.text("Sensor Value :n ", 0, 0);
  // ste the font size very large for the loop
  TFTscreen.setTextSize(5);
}

void loop() {

  // Read the value of the sensor on A0
  String sensorVal = String(analogRead(A0));

  // convert the reading to a char array
  sensorVal.toCharArray(sensorPrintout, 4);

  // set the font color
  TFTscreen.stroke(255, 255, 255);
  // print the sensor value
  TFTscreen.text(sensorPrintout, 0, 20);
  // wait for a moment
  delay(250);
  // erase the text you just wrote
  TFTscreen.stroke(0, 0, 0);
  TFTscreen.text(sensorPrintout, 0, 20);
}

I’m using the analog version of that code to print information from a temperature sensor to the TFT LCD, but I’d also like to print values from ultrasonic range finder (URF) – that uses a digital pin.

At first, the URF and TFT LCD needed the same digital pin (pin 7) according to the code I used. I figured I couldn’t get the digital value to print because the two used the same pin—since the URF didn’t work when using any other pin, I switched the “cs” pin of the TFT to digital pin 4. It still displays fine, other than the absence of the URF’s value.

Context:

  • Board: Arduino Leonardo
  • TFT Display/LCD Module with SD Card Reader (http://arduino.cc/en/Main/GTFT)
  • Serial Port: /dev/tty.usbmodem1421
  • Arduino version 1.0.6
  • Mac OS X Version 10.9.5
  • Goal: get LCD Display to read from ultrasonic range finder.

My Code: (I deleted the serial print parts and all others I deemed not relevant)

float temp;
int tempPin = 1;
const int pingPin = 7;
//int val = 0;


#include <TFT.h>  // Arduino LCD library
#include <SPI.h>

// pin definition for the Leonardo
#define cs   4 //changed from pin 7 to pin 4 since range finder also uses pin 7...
#define dc   0
#define rst  1

// defining "scope" myScreen OR ... in Arduino's words: // create an instance of the library
TFT myScreen = TFT(cs, dc, rst);

// char array to print to the screen
char tempPrintout[6]; //[4] gives xx._, [5] gives xx.x

char soundPrintout[6]; //****For the URF!

void setup() {

  // Put this line at the beginning of every sketch that uses the GLCD:
  myScreen.begin();
  myScreen.background(0, 0, 0);
  myScreen.stroke(255, 255, 255);
  myScreen.setTextSize(2);
  myScreen.text("Sensor Value :n ", 0, 0);
  myScreen.setTextSize(1);
  delay(1000); 
  
  Serial.begin(9600);

  
}

void loop() {
  
  // Read the value of the sensor on A1 (Temp sensor)
  String tempVal = String(analogRead(A1)* 0.48828125);

  // convert the reading to a char array
  tempVal.toCharArray(tempPrintout, 6); //changes decimal places
  
    // Read the value of the sensor on.....pin 7???
//  String soundVal = String(digitalRead(7)/29/2); here, I tried just putting in the #7 with digital read
  String soundVal = String(digitalRead(pingPin)); //this also doesn't work


  //convert the reading to a char array
  soundVal.toCharArray(soundPrintout, 6); //not sure if this should work

  myScreen.stroke(255, 255, 255);
  myScreen.text(tempPrintout, 80, 20);
  // wait for a moment
  delay(3000);
  // erase the text you just wrote
  myScreen.stroke(0, 0, 0);
  myScreen.text(tempPrintout, 80, 20);
  
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, cm; //inches

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH );
  
  myScreen.text("SEA LEVEL", 2, 40);
  myScreen.text(soundPrintout, 10, 40 //???

  // convert the time into a distance
  //inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  delay(100);
}

 
long microsecondsToCentimeters(long microseconds)
{
//  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
//  // The ping travels out and back, so to find the distance of the
//  // object we take half of the distance travelled.
 return microseconds / 29 / 2;
}

Yes, you can treat the analogue input pins as digital.

You would also need to set the pinMode for those pins too, I believe analogRead does not require the pinMode to be set.

alphauk101: You would also need to set the pinMode for those pins too, I believe analogRead does not require the pinMode to be set.

How exactly would I go about doing that? I've tried to insert "INPUT"/"OUTPUT"/"HIGH"/"LOW" where the asterisks are

String soundVal = String(digitalRead(pingPin, ****))

But, apparently, the String function doesn't work like that. Would I just put a pinMode(pin, mode) line after the String, or before the String then define soundVal some other way? I've done some reading on string/String, and am starting to wonder if I'm using the right one for the digital pin.

I don't want to come across ignorant or anything, it's just that Arduino's pages are sometimes harder to follow, than helpful, for a newbie like me. :)

Thanks for all the replies (in advance)!

  1. Read the documentation - the digialRead has only one param!

  2. Yes you can change a digialRead for an analogRead or vice versa BUT unless you make a whole lot of other changes (hardware and software) it will make no sense - it's not just the SYNTAX that matters.

Mark

pomegracious: How exactly would I go about doing that?

pinMode(pin, mode) is a separate function, usually called in setup().