[Solved] Graph Plot in spark

Hello. I have written a program in spark which shows the out put values getting from a photoresistor. Now i wanna plot a graph according the photoresistor values. Is it possible please let me know how?
here is my program.

Original Picture of Code

int lightPin = A0; // define a pin for the Photo resistor
int ledPin = D6; // define a pin for an LED

void setup()
{
    Serial.begin(9600);
    pinmode(ledPin, OUTPUT);
}

void loop()
{
    Serial.println(analogRead(lightPin));
    analogWrite(ledPin, analogRead(lightPin));
    delay(1000);
}

Next time, please just copy&paste the code. It makes it a lot easier for us to duplicate your result, and test things. Doing so, please take a look at this post so you know how to properly format your code. Thanks in advance! ~Jordy

I went ahead and converted the image into a code block for easy reading and for search indexing. -Harrison

It is note possible to do that directly using the SparkIDE. You will need to use some support library. I would suggest looking at Plotly. Take a look at this thread: http://community.spark.io/t/spark-core-plotly/4782 and let me know if you have additional questions.

Hey @Subh,
Is it possible to create a graph from measured data? -Yes
Is it possible to do this using a native Spark command? -No (not yet at least)
Fortunately, there are countless methods through which you can create graphs of your gathered data. Those depend on the language you’d like to use, the platform on which you’d like to run it, and some other factors. There are two parts to consider; you need some sort of database to store the data in, and then you need something to display that data. Luckily, there are several of these services to be found on the web, some specifically made for the IoT. One in particular is http://atomiot.com made with the Spark in mind. Here you can easily add your Core, and create graphs without a hassle. Definitely check it out!


You should also take another look at your code, since it isn’t going to work. You’re not going to get a measurement in degrees, but rather as a number from 0-4095 so that won’t be that nice in a graph.
While the above is merely cosmetical, a greater problem presents itself here:

analogWrite(ledPin, analogRead(lightPin));

You’re doing an analogWrite to a pin, with a value between 0-4095. analogWrite, however, is a PWM function which only supports values between 0-255.
Also, you’re doing an analogWrite to a Digital pin (int ledPin = D6), which could potentially lead to more problems.
So you might want to take a second look at that.

1 Like

Note, you can do an analogWrite to a digital pin but not all digital pins. From the documentation:

When you want to use the analogWrite() function on the Core, for instance to smoothly dim the brightness of LEDs, you need to use pins that have a timer peripheral. People often call these PWM pins, since what they do is called Pulse Width Modulation. The Core has 8 PWM pins: A0, A1, A4, A5, A6, A7, D0 and D1.

As to the 4096 to 255 issue you need to use map(). Here's an example (again from the documentation):

int val = analogRead(lightPin);
val = map(val, 0, 4095, 0, 255);
analogWrite(ledPin, val);
1 Like

Thanks for your comments. Now its works greatly.