I am asked to complete the following assignment.
For this program, students will be using the built-in light sensor (A8).
Write a program that can measure 100 samples of the light sensor with 100 ms between each sample.
Use the serial plotter to plot the data before and after a 10 element moving average.
Try different window sizes and observe the differences.
This is what I have so far:
#include <math.h> //include the math.h library
int PhotoPin = A8; //Photo Sensor pin definition
float V0; //Voltage from photo sensor -y-value
int Lux; //output variable -x-xvalue
float m; //Slope calculated from data sheet
int L[100]; //Preallocates memory to speed up compiling and execution
void setup() {
Serial.begin(9600); //Starting serial communication
while (!Serial); //While there is no serial communication, just chill out dude
pinMode(PhotoPin, INPUT); // Proper input statement to build habits
}
void loop() {
for(int i = 0; i<100; i++) { //Note the range is technically from 0-99
V0 = analogRead(PhotoPin); //Input voltage in mV from sensor
m = (4.5-2.3)/(4000-2000); //Calculate slope rise over run
V0 = map(V0, 0, 1023, 0, 4600); //Rescales the "voltage" from our A to D conveter to read in millivolts //Serial.print("m is: "); //Serial.println(m);
Lux = (V0/1000)/m; //Lux is the x value from the rearranged equation in the data sheet's graph
L = Lux;
Serial.print("The illumination level is: ");*
Serial.print(Lux);*
Serial.println(" Lux");*
delay(100);* } } I'm not sure what I need to add, if there is anything, in order to complete this assignment. Please help.
Is your code really in italics? Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.
You may not longer send text, such as: Serial.println( "Hello");
There is a programmer's trick for a "moving average" (or walking average or running average) when integers are used.
Arduino IDE menu: File / Examples / 03.Analog / Smoothing
Also here: https://www.arduino.cc/en/Tutorial/BuiltInExamples/Smoothing.
You can also grab the last 10 samples and calculate the average.
Give it a try and show us your sketch.
Can you tell the compiler when it should calculate something with integers, long, float, and so on. You mix up everything as if it is not important.
The analogRead() function returns an integer. You can start with that.
Please allow me some sarcasm. Is that a management course assignment?
I am not a big fan of solving student assignments for you and I am probably not the only one. We are here to help users learn solving problems themselves first. So, if you have a specific question after you tried to solve the problem yourself and read some documentation please feel free to ask. Otherwise, what are you going to do when you finish your course and get a real task/job.