The problem is, since tempPin is analog pin 2 and also redPin is digital pin 2. Will arduino itself know which is analog pin 2 and digital pin 2. Or is there a way to set pin mode.
You only need to set the pinMode for a Analog pin when you want to use it as a Digital I/O.
If you want to use it as a Digital I/O use the aliases "A0, A1, A2..". The compiler will convert those to the correct pin numbers, e.g. 14, 15, 16 on the Uno.