Serial.println(updatedPos);
on the serial monitor i am getting the error:"Initializing SD card...initialization failed!"
when there is no use of the SD card anywhere in my code and i am very confused... any help would be much appreciated, thank you
Welcome to the forum
As your topic does not relate directly to the installation or operation of the IDE it has been moved to the Programming Questions category of the forum
Please provide more details of what you are doing. Post your sketch here, using code tags when you do, and tell us which Arduino board you are using
In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.
Maybe start with sharing the code, that would certainly help.
Maybe in a library that you use.
I would also be
hello, sorry this took a few days ! here is the code. i am working on a free course from edx called "introduction to haptics" and i have been creating a haptic device. the purpose of the bit i am trying to do is to read the position sensoring on my hapkit device by printing it to the serial monitor. i have only changed one peice of code and the rest was given, but it should work at this point... thank you
//--------------------------------------------------------------------------
// Tania Morimoto and Allison Okamura, Stanford University
// 11.16.13 - 10.16.14
// Code to test basic Hapkit functionality (sensing and force output)
//--------------------------------------------------------------------------
// Includes
#include <math.h>
// Pin declares
int pwmPin = 5; // PWM output pin for motor 1
int dirPin = 8; // direction output pin for motor 1
int sensorPosPin = A2; // input pin for MR sensor
// Position tracking variables
int updatedPos = 0; // keeps track of the latest updated value of the MR sensor reading
int rawPos = 0; // current raw reading from MR sensor
int lastRawPos = 0; // last raw reading from MR sensor
int lastLastRawPos = 0; // last last raw reading from MR sensor
int flipNumber = 0; // keeps track of the number of flips over the 180deg mark
int tempOffset = 0;
int rawDiff = 0;
int lastRawDiff = 0;
int rawOffset = 0;
int lastRawOffset = 0;
const int flipThresh = 700; // threshold to determine whether or not a flip over the 180 degree mark occurred
boolean flipped = false;
// Kinematics variables
double xh = 0; // position of the handle [m]
// Force output variables
double force = 0; // force at the handle
double Tp = 0; // torque of the motor pulley
double duty = 0; // duty cylce (between 0 and 255)
unsigned int output = 0; // output command to the motor
// --------------------------------------------------------------
// Setup function -- NO NEED TO EDIT
// --------------------------------------------------------------
void setup()
{
// Set up serial communication
Serial.begin(9600);
// Set PWM frequency
setPwmFrequency(pwmPin,1);
// Input pin
pinMode(sensorPosPin, INPUT); // set MR sensor pin to be an input
// Output pins
pinMode(pwmPin, OUTPUT); // PWM pin for motor A
pinMode(dirPin, OUTPUT); // dir pin for motor A
// Initialize motor
analogWrite(pwmPin, 0); // set to not be spinning (0/255)
digitalWrite(dirPin, LOW); // set direction
// Initialize position valiables
lastLastRawPos = analogRead(sensorPosPin);
lastRawPos = analogRead(sensorPosPin);
}
// --------------------------------------------------------------
// Main Loop
// --------------------------------------------------------------
void loop()
{
//*************************************************************
//*** Section 1. Compute position in counts (do not change) ***
//*************************************************************
// Get voltage output by MR sensor
rawPos = analogRead(sensorPosPin); //current raw position from MR sensor
// Calculate differences between subsequent MR sensor readings
rawDiff = rawPos - lastRawPos; //difference btwn current raw position and last raw position
lastRawDiff = rawPos - lastLastRawPos; //difference btwn current raw position and last last raw position
rawOffset = abs(rawDiff);
lastRawOffset = abs(lastRawDiff);
// Update position record-keeping vairables
lastLastRawPos = lastRawPos;
lastRawPos = rawPos;
// Keep track of flips over 180 degrees
if((lastRawOffset > flipThresh) && (!flipped)) { // enter this anytime the last offset is greater than the flip threshold AND it has not just flipped
if(lastRawDiff > 0) { // check to see which direction the drive wheel was turning
flipNumber--; // cw rotation
} else { // if(rawDiff < 0)
flipNumber++; // ccw rotation
}
if(rawOffset > flipThresh) { // check to see if the data was good and the most current offset is above the threshold
updatedPos = rawPos + flipNumber*rawOffset; // update the pos value to account for flips over 180deg using the most current offset
tempOffset = rawOffset;
} else { // in this case there was a blip in the data and we want to use lastactualOffset instead
updatedPos = rawPos + flipNumber*lastRawOffset; // update the pos value to account for any flips over 180deg using the LAST offset
tempOffset = lastRawOffset;
}
flipped = true; // set boolean so that the next time through the loop won't trigger a flip
} else { // anytime no flip has occurred
updatedPos = rawPos + flipNumber*tempOffset; // need to update pos based on what most recent offset is
flipped = false;
}
//*************************************************************
//*** Section 2. Compute position in meters *******************
//*************************************************************
// ADD YOUR CODE HERE
Serial.println(updatedPos); //print the updated position (analogue value) to the sscreen as decimal. i think take seria port as that where info is
// Define kinematic parameters you may need
//double rh = ?; //[m]
// Step 2.1: print updatedPos via serial monitor
// Step 2.6: double ts = ?; // Compute the angle of the sector pulley (ts) in degrees based on updatedPos
// Step 2.7: xh = ?; // Compute the position of the handle (in meters) based on ts (in radians)
// Step 2.8: print xh via serial monitor
//*************************************************************
//*** Section 3. Assign a motor output force in Newtons *******
//*************************************************************
// ADD YOUR CODE HERE
// Define kinematic parameters you may need
//double rp = ?; //[m]
//double rs = ?; //[m]
// Step 3.1: force = ?; // In lab 3, you will generate a force by simply assigning this to a constant number (in Newtons)
// Step 3.2: Tp = ?; // Compute the require motor pulley torque (Tp) to generate that force
//*************************************************************
//*** Section 4. Force output (do not change) *****************
//*************************************************************
// Determine correct direction for motor torque
if(force < 0) {
digitalWrite(dirPin, HIGH);
} else {
digitalWrite(dirPin, LOW);
}
// Compute the duty cycle required to generate Tp (torque at the motor pulley)
duty = sqrt(abs(Tp)/0.0125); // the constant is 0.0125 for the Mabuchi motor and 0.03 for the Maxon A-max motor)
// Make sure the duty cycle is between 0 and 100%
if (duty > 1) {
duty = 1;
} else if (duty < 0) {
duty = 0;
}
output = (int)(duty* 255); // convert duty cycle to output signal
analogWrite(pwmPin,output); // output the signal
}
// --------------------------------------------------------------
// Function to set PWM Freq -- DO NOT EDIT
// --------------------------------------------------------------
void setPwmFrequency(int pin, int divisor) {
byte mode;
if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 64: mode = 0x03; break;
case 256: mode = 0x04; break;
case 1024: mode = 0x05; break;
default: return;
}
if(pin == 5 || pin == 6) {
TCCR0B = TCCR0B & 0b11111000 | mode;
} else {
TCCR1B = TCCR1B & 0b11111000 | mode;
}
} else if(pin == 3 || pin == 11) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 32: mode = 0x03; break;
case 64: mode = 0x04; break;
case 128: mode = 0x05; break;
case 256: mode = 0x06; break;
case 1024: mode = 0x7; break;
default: return;
}
TCCR2B = TCCR2B & 0b11111000 | mode;
}
}
these are the instructions too
- In Section 2 of loop(), add code to print to the value of the variable updatedPos to screen. Hint: Use the function Serial.println() with the appropriate argument(s).
- After you have uploaded your program to the Hapkit Board (and keeping the USB plugged in), use the Arduino IDE's built-in serial monitor to view the result as you move the handle around. Hint: click on Tools > Serial Monitor in the Arudino IDE. You should see the values change as you move the handle around. Checking the box that says "Autoscroll" will allow you to see the latest measured value at the bottom of the serial monitor window. (Whenever a new value is printed, all the previous values are shifted upward.)
I am using Hapkit Board SKU 102990020 by seed studio and I suspect it could be on my end having wrong drivers or using the wrong program, but I have tested with the ones available and thought I was ok. Thank you
So.....show the original in full.
Show your upload compiler messages.
Tell what board you are using and possibly a circuit diagram.
Maybe.
The thing is, the code you posted does not include any line that would produce the output you mentioned: "Initializing SD card...initialization failed!"
So the logical conclusion is that you did not upload the code you posted to your Arduino board.
+1 for your conclusion.
I suspect that if the OP now (carefully) posts both the original full code, and the code that delivers the initialisation error message, he/she will spot the cause.
BTW, the message curiously looks like one in a function of the DFR MP3 Player.
hello, i am so sorry for this mistake! thank you so much... lifesaver