I am working in the arduino environment on code that will record data and create
ID: 3765956 • Letter: I
Question
I am working in the arduino environment on code that will record data and create code that reads the sensor and maps it to a byte value every two seconds store a counter timestamp and the analog sensor value to the onboard EEPROM for. And do this for at least two minutes. The readings should be two minutes apart. The count timestamp just keeps track of the number of readings (1, 2, 3, ... 60). So for example, your data in eeprom should look like 0 34 1 45 2 56 ......... this is my code so far, but the data isn't reading properly. I am trying to figure out what I need to do to make it record and read the sensor values properly:
#include <TimerOne.h>
#include <EEPROM.h>
// Motor definitions to make life easier:
#define MOTOR_A 0
#define MOTOR_B 1
// Clockwise and counter-clockwise definitions.
// Depending on how you wired your motors, you may need to swap.
#define FORWARD 0
#define BACKWARD 1
// Pin Assignments //
// Don't change these! These pins are statically defined by shield layout
const byte PWMA = 3; // PWM control (speed) for motor A
const byte PWMB = 11; // PWM control (speed) for motor B
const byte DIRA = 12; // Direction control for motor A
const byte DIRB = 13; // Direction control for motor B
const int sensorMin = 600; // sensor minimum,
const int sensorMax = 900; // sensor maximum,
volatile unsigned long blinkCount = 0; // use volatile for shared variables
// Timer for EEPROM read //
const long interval = 2000; // interval at which to wait 2 seconds (milliseconds)
unsigned long previousMillis = 0; // will store last time LED was updated
const int led = LED_BUILTIN; // the pin with a LED
int addr = 0;
int val = analogRead(A0);
void setup() {
// initialize serial communication:
Serial.begin(9600);
pinMode(led, OUTPUT);
Timer1.initialize(1000000);
Timer1.attachInterrupt(blinkLED); // blinkLED to run every 2 seconds
Serial.begin(9600);
setupArdumoto(); // Set all pins as outputs
}
void blinkLED(void)
{
blinkCount = blinkCount + 1; // increase when LED turns on
}
void writeEEPROM (void)
//set up EEPROM write
{
EEPROM.write(addr, val);
// Serial.print(addr);
Serial.print("Value ");
Serial.println(val);
Serial.print("A0 ");
Serial.println(analogRead(A0));
addr = addr + 1;
if(addr == EEPROM.length())
break;
}
void loop() {
// read the sensor:
int sensorReadingLeft = analogRead(A0);
int sensorReadingCenter = analogRead(A1);
int sensorReadingRight = analogRead(A2);
// map the sensor range to a range of four options:
int rangeLeft = map(sensorReadingLeft, sensorMin, sensorMax, 0, 1);
int rangeCenter = map(sensorReadingCenter, sensorMin, sensorMax, 0, 1);
int rangeRight = map(sensorReadingRight, sensorMin, sensorMax, 0, 1);
// do something different depending on the
// range value:
int left = analogRead(A0); // white = 0 black = 1
int center = analogRead(A1);
int right = analogRead(A2);
if (analogRead(A5)>= 1000) {
readEEPROM();
}
// On the line
if ((left <= 800) && (center >800) && (right <= 800)){
driveArdumoto(MOTOR_A, FORWARD, 210); // Motor A at max speed.
driveArdumoto(MOTOR_B, FORWARD, 210); // Motor B at max speed.
}
// turn left
if ((left > 800) && (center <= 800) && (right <= 800)){
driveArdumoto(MOTOR_A, FORWARD, 175); // Motor A at max speed.
driveArdumoto(MOTOR_B, 0,0 ); // Motor B at max speed.
}
// turn right
if ((left <= 800)&& (center <= 800) && (right > 800)){
driveArdumoto(MOTOR_A, 0, 0); // Motor A at max speed.
driveArdumoto(MOTOR_B, FORWARD, 175); // Motor B at max speed.
}
//
if ((left <= 800) && (center <= 800) && (right <=800)){
driveArdumoto(MOTOR_A, BACKWARD, 175); // Motor A reverse
driveArdumoto(MOTOR_B, BACKWARD, 175); // Motor B reverse
}
// else {
// driveArdumoto(MOTOR_A, 0, 0); // Motor A stop.
// driveArdumoto(MOTOR_B, 0, 0); // Motor B stop.
// }
unsigned long blinkCopy; // holds a copy of the blinkCount
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
writeEEPROM();
// save the last time you wrote to EEPROM
previousMillis = currentMillis;
}
} //Void Loop end
// delay(500); // delay in between reads for stability
void loopMotor()
{
Serial.println(analogRead(A0));
if (analogRead(A0) > 900){
driveArdumoto(MOTOR_A, 0, 0);
driveArdumoto(MOTOR_B, 0, 0);
}
else {
driveArdumoto(MOTOR_A, FORWARD, 255); // Motor A at max speed.
driveArdumoto(MOTOR_B, FORWARD, 255); // Motor B at max speed.
}
} // close Void Loop
// driveArdumoto drives 'motor' in 'dir' direction at 'spd' speed
void driveArdumoto(byte motor, byte dir, byte spd)
{
if (motor == MOTOR_A)
{
digitalWrite(DIRA, dir);
analogWrite(PWMA, spd);
}
else if (motor == MOTOR_B)
{
digitalWrite(DIRB, dir);
analogWrite(PWMB, spd);
}
}
void readEEPROM()
{
addr = 0;
byte value = EEPROM.read(addr);
// EEPROM.read(addr, val);
// Serial.print(addr);
// Serial.print("Address ");
// Serial.print(addr);
// Serial.print(" ");
// Serial.print("Value ");
// Serial.println(value);
// Serial.print("A0 ");
// Serial.println(analogRead(A0));
addr = addr + 1;
if(addr == EEPROM.length())
addr = 0;
}
// setupArdumoto initialize all pins
void setupArdumoto()
{
// All pins should be setup as outputs:
pinMode(PWMA, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(DIRA, OUTPUT);
pinMode(DIRB, OUTPUT);
// Initialize all pins as low:
digitalWrite(PWMA, LOW);
digitalWrite(PWMB, LOW);
digitalWrite(DIRA, LOW);
digitalWrite(DIRB, LOW);
}
Explanation / Answer
Sometimes when you’re prototyping a project it’s nice to see what’s going on with your sensors or variables in your code. Sure you can watch analogRead() values scroll at warp speed on your Arduino Serial Monitor, but what if you want to save that data? Or look at a graph you make in real time instead of numbers? Maybe you want to do some self tracking and visualize your Fitbit stats, or compare two different light sensors or force sensing resistors? In this class I’ll demonstrate two workflows that help us do this…
The Basics
Check out the Analog In lab. Using this code:
int sensorPin = A0; // analog input pin to hook the sensor to
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
Serial.begin(9600); // initialize serial communications
}
void loop() {
sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.println(sensorValue); // print value to Serial Monitor
delay(50); // short delay so we can actually see the numbers
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.