Help With Map Function

Im Trying To Display The Light Intensity Percentage Reading From A Light Sensor Heres My code But Its Not Working!!! Can Someone Proofread And Tell Me Why Its Not Working!!

Thanks In Advance!!

void setup() {}

void loop() { int lightPin = 0 ; //Photoresistor Pin int val = analogRead(lightPin); //Read Pin val = map(val, 0, 1023, 0, 100); //Convert Value From Pin To Percent Serial.println("Light Intencity" val"%") ; // Display Percent Delay (1000) }

What type of light sensor are you using? How diod you connect it to the Arduino?

I refactored your code so it should run (tested on Arduino IDE 0.22)

void setup() 
{
  Serial.begin(115200);
  Serial.println("start...");
}

void loop()
{
  int lightPin = 0 ;  //Photoresistor Pin

  int val = analogRead(lightPin);

  val = map(val, 0, 1023, 0, 100);   
        
  Serial.print("Light Intencity");
  Serial.print(val);
  Serial.println("%");

  delay (1000);
}

Orleymtnez: Can Someone Proofread And Tell Me Why Its Not Working!!

No because you have provided no description of what is actually happening. So how can anyone suggest a problem?

Im using the basic photoresistor Sensor on an arduino mega 2560 with sensor shield on pin 46 and the code wont go through!!! also for some reason the new code that robtillaart gave me will go through but just gives weird giberish any thoughts??

Again, you aren't explaining what you are seeing.

What does "code won't go through" mean? Through what? The wall? Code doesn't do that.

Are you getting error messages? What are those (exact) error messages. What does "gibberish" mean? I don't see anywhere in his code that it says to print the word "gibberish".

I think robillart found your major problem, ie, forgot to setup the Serial port function.
Also you’ll notice that Arduino does not support normal printf() operations like:

 Serial.println("Light Intencity" val"%") ;  // Display Percent

To deal with this issue, I defined a number of simple printf() functions to handle the most common
situations, Saves several tons of typing, eg

/*
  trivial printf for string/integer/string display. 
  - usage:  printf_sis( "...", -123, "..." );
*******************************************************/
void printf_sis( char* s1, int num, char* s2 )
{
  Serial.print( s1 );
  Serial.print( num );
  Serial.print( s2 );  
}

Also, looks like robillart’s sketch should work. You possibly have your photoresistor hooked
up wrong. You need a voltage-divider,

+5v → photoresistor <-±> 10K resistor → gnd

A/D connects at <-±>.

i guess it must be faulty because its one of those ebay plug and play sensor modules for the sensor shield. Do you think it could be??

Im using the mega 2560 set up with the sensor shield v4 on and its pluged in on pin 46 (I think thats an analog pin)

This is the Description Description: The Light Sensor brick Module is based on photoresistor, It measures the environment light intensity and input data to Arduino through the analog IO port of Sensor Shield. Great tool for light following robot, gardening system or home alert system. Building interactive work is as easy as piling bricks, just plug it to Arduino Sensor Shield with a buckled cable, and make it looks professional and neat.

Im I Supposed to make conect it to one of the A1, A2, A3 pins??? if so how would where in the code could i use the printf function ive never heard of it before??

The code works but on the serial it will display []YF[U etc... (Not the exact message displaying but some gibberish like that) i got a feeling my map function is wrong thats why im getting those results......

Orleymtnez:
Im using the mega 2560 set up with the sensor shield v4 on and its pluged in on pin 46 (I think thats an analog pin)

Hint: Pin 46 isn’t an analog pin.

If you have reasonable looking Serial.print statements [ie, such as those written for you by robtilaart] and you're seeing gibberish, it's 90% probable you don't have the baudrate of your terminal emulator [Serial Monitor] set the same as the baudrate of the Arduino. [eg, try 57600 in the code instead of 115200, or change the Serial Monitor to 115200].