I am simulating a very simple program (on TinkerCAD).
It is an LCD screen, and if a switch is flipped, a new message appears. I even shorted the 3.3 (above the 2V minimum high), and still no luck.
/*
LiquidCrystal Library - Hello World
Demonstrates the use a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
This sketch prints "Hello World!" to the LCD
and shows the time.
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/
#include <LiquidCrystal.h>
const byte pin8 = 8;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Welcome.");
}
void loop() {
if(pin8 == true)
{
//lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Display new item");
}
else
{
lcd.setCursor(0, 1);
lcd.print("Loop works.");
}
}
Thanks. The code works.
It's just, now, TinkerCAD's slide switch is somehow always on.
The circuit is never open with the switch in there. I moved the switch to the left side too, and the same problem. In the attachment, it should read "Loop works." The serial monitor confirms that it's always true with the slide switch (even when toggled).
adamaero:
Thanks. The code works.
It's just, now, TinkerCAD's slide switch is somehow always on.
The circuit is never open with the switch in there. I moved the switch to the left side too, and the same problem. In the attachment, it should read "Loop works." The serial monitor confirms that it's always true with the slide switch (even when toggled).
That is because the slide switch connects the centre pin to either the left or right pins depending on where the slider is. You have your input (pin 8 ) on the left pin, nothing on the centre pin and 3.3V on your right pin. The input is always left floating. It looks like tinkercad assumes a floating input reads high.
The best way to design your circuit to to add the following line to your setup() function:-
pinMode(pin8, INPUT_PULLUP);
This attaches an internal 10k ohm resistor between pin 8 and +5V, Which ensures that when nothing is connected to pin 8 it is pulled firmly high.
Then remove the +3.3V connection from the switch right pin and add a connection from the switch centre pin to ground.
This way when the slider is to the left the input will be taken to ground through the switch and will read LOW to the software. When the switch slider is to the right the internal resistor in the Arduino will pull the pin to +5V and it will read HIGH.
Funny, I already took a logic class where every lab used floating DIP switches handled by pull down resistors. I guess it just didn't occur to me that this applies to all types of switches.
=)