can any one advise why the counter is not incrementing ?
code is not finished yet,
thanks for help in advance
/*
SD card datalogger
This example shows how to log data from three analog sensors
to an SD card using the SD library.
The circuit:
* analog sensors on analog ins 0, 1, and 2
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
*/
//#include <LiquidCrystal.h>
#include <SD.h>
const int chipSelect = 4;
int x0 = analogRead(A0); //Reads the analog value on pin A0 into x0
int x1 = analogRead(A1); //Reads the analog value on pin A1 into x1
int x2 = analogRead(A2); //Reads the analog value on pin A2 into x2
int x3 = analogRead(A3); //Reads the analog value on pin A3 into x3
int x4 = analogRead(A4); //Reads the analog value on pin A4 into x4
int x5 = analogRead(A5); //Reads the analog value on pin A5 into x5
int volts0 = 0; // to store AO dc voltage level 1 or 0
int volts1 = 0; // to store A1 dc voltage level
int volts2 = 0; // to store A2 dc voltage level
int volts3 = 0; // to store A3 dc voltage level
int volts4 = 0; // to store A4 dc voltage level
int volts5 = 0; // to store A5 dc voltage level
int laststatevolts0 = 0;
int laststatevolts1 = 0;
int laststatevolts2 = 0;
int laststatevolts3 = 0;
int laststatevolts4 = 0;
int laststatevolts5 = 0;
int counter0 = 0; // the counters keeps track of how many times input (AO)has gone high
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
int counter5 = 0;
int s0 = 0 ;
const int threshold = 1;
const int ref = 950;
void setup()
{
pinMode(A0, INPUT); // set up pins as voltage inputs
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(A5, INPUT);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
;
}
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void loop()
{
Serial.println (" counter 1 is at ");
Serial.println (counter1);
delay(100);
// make a string for assembling the data to log:
String dataString = "";
// read six sensors and append to the string:
for (int analogPin = 0; analogPin < 6; analogPin++) {
int sensor = analogRead(analogPin);// to change to ro to r5
dataString += String(sensor);
if (analogPin < 5) {
dataString += ",";
}
}
// look at Analog pins and determin state//
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
int volts0 = (analogRead(A0) /1001); //read Analog X0 if 5v =1 0v = 0 in volts0
int volts1 = (analogRead(A1) /1001); //read Analog X1
int volts2 = (analogRead(A2) /1001); //read Analog X2
int volts3 = (analogRead(A3) /1001); //read Analog X3
int volts4 = (analogRead(A4) /1001); //read Analog X4
int volts5 = (analogRead(A5) /1001); //read Analog X5
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString); // print to the serial port too:
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
Serial.println (volts0);
Serial.println (volts1);
Serial.println (volts2);
Serial.println (volts3);
Serial.println (volts4);
Serial.println (volts5);
delay (2000);
if (analogRead(A0) < ref ){ // need to compaire if high or low
Serial.println ("A0 is low ");
s0 = 0;
}
else {
Serial.println ("A0 is high ");
s0 = 1;
Serial.println (s0);
}
if (analogRead(A1) < ref){ // need to compaire if high or low
Serial.println ("A1 is low ");
}
else {
Serial.println ("A1 is high ");
delay(200);
}
if (analogRead(A2) < ref) { // need to compaire if high or low
Serial.println ("A2 is low ");
}
else {
Serial.println ("A2 is high ");
delay(200);
}
if (analogRead(A3) < ref){ // need to compaire if high or low
Serial.println ("A3 is low ");
}
else {
Serial.println ("A3 is high");
delay(200);
}
if (analogRead(A4) < ref){ // need to compaire if high or low
Serial.println ("A4 is low");
}
else {
Serial.println ("A4 is high ");
delay(200);
}
if (analogRead(A5) < ref){ // need to compaire if high or low
Serial.println ("A5 is low ");
}
else {
Serial.println ("A5 is high ");
delay(200);
delay(200);
Serial.println ("s0 is");
if (volts1 != laststatevolts1) { //(volts1 != laststatevolts1) {
if (volts1 != 0){
counter1++;
}
else
{
laststatevolts1 = volts1;
}
}
}
}
Using the auto-format tool in the IDE might help you highlight your problem.
Hint: When you find yourself writing stuff like this
int volts0 = 0; // to store AO dc voltage level 1 or 0
int volts1 = 0; // to store A1 dc voltage level
int volts2 = 0; // to store A2 dc voltage level
int volts3 = 0; // to store A3 dc voltage level
int volts4 = 0; // to store A4 dc voltage level
int volts5 = 0; // to store A5 dc voltage level
There are over 200 lines in that code. Which line is the errant counter on?
If you need to figure out how make a counter increment you can do so with a program of 10 lines or so. Then when it works you can bring your new knowledge into the bigger program.
If this is the section of code that you are having problems with what do you see if you print the value of volts1 and laststatevolts1 before testing them ?
int volts1 = (analogRead(A1) /1001); //read Analog X1
If analogRead(A1) is returning a value less than 1001 then volts1 will always be 0 and the counter will never be incremented. Do you have A1 connected to +5V (or close to it)?
thanks for the help , yes you are correct , when 5v is on the pin it is supposed to increment , when 0v is on the pin it will not. that my problem, I put 5v on the pin yes it know there is 5v..but the countervolts1 will not increment.
A5 ? ? ? I am just trying to get A1 going to start then all the A0-A5 pins will be the same, I am trying to make a logger, and keep the digital pins free for an lcd.
the go is when A1 is high, volts1 should = 1 and then increment the countervolts1 but is not.
if (analogRead(A5) < ref){ // need to compaire if high or low
Serial.println ("A5 is low ");
}
else {
Serial.println ("A5 is high ");
delay(200);
delay(200);
Serial.println ("s0 is");
if (volts1 != laststatevolts1) { //(volts1 != laststatevolts1) {
if (volts1 != 0){
counter1++;
}
else
{
laststatevolts1 = volts1;
}
}
}
// Changed your code segment to this one:
/// and it would solve your problem !
if (volts1 != laststatevolts1) { //(volts1 != laststatevolts1) {
laststatevolts1 = volts1;
if (volts1 != 0){
counter1++;
}else{
// do nothing
}
}
/*
SD card datalogger
This example shows how to log data from three analog sensors
to an SD card using the SD library.
The circuit:
* analog sensors on analog ins 0, 1, and 2
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
*/
//#include <LiquidCrystal.h>
#include <SD.h>
const int chipSelect = 4;
int x0 = analogRead(A0); //Reads the analog value on pin A0 into x0
int x1 = analogRead(A1); //Reads the analog value on pin A1 into x1
int x2 = analogRead(A2); //Reads the analog value on pin A2 into x2
int x3 = analogRead(A3); //Reads the analog value on pin A3 into x3
int x4 = analogRead(A4); //Reads the analog value on pin A4 into x4
int x5 = analogRead(A5); //Reads the analog value on pin A5 into x5
int volts0 = 0; // to store AO dc voltage level 1 or 0
int volts1 = 0; // to store A1 dc voltage level
int volts2 = 0; // to store A2 dc voltage level
int volts3 = 0; // to store A3 dc voltage level
int volts4 = 0; // to store A4 dc voltage level
int volts5 = 0; // to store A5 dc voltage level
int laststatevolts0 = 0;
int laststatevolts1 = 0;
int laststatevolts2 = 0;
int laststatevolts3 = 0;
int laststatevolts4 = 0;
int laststatevolts5 = 0;
int counter0 = 0; // the counters keeps track of how many times input (AO)has gone high
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
int counter5 = 0;
int s0 = 0 ;
const int threshold = 1;
const int ref = 950;
void setup()
{
pinMode(A0, INPUT); // set up pins as voltage inputs
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(A5, INPUT);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
;
}
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void loop()
{
Serial.println (" counter 1 is at ");
Serial.println (counter1);
delay(100);
// make a string for assembling the data to log:
String dataString = "";
// read six sensors and append to the string:
for (int analogPin = 0; analogPin < 6; analogPin++) {
int sensor = analogRead(analogPin);// to change to ro to r5
dataString += String(sensor);
if (analogPin < 5) {
dataString += ",";
}
}
// look at Analog pins and determin state//
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
int volts0 = (analogRead(A0) /1001); //read Analog X0 if 5v =1 0v = 0 in volts0
int volts1 = (analogRead(A1) /1001); //read Analog X1
int volts2 = (analogRead(A2) /1001); //read Analog X2
int volts3 = (analogRead(A3) /1001); //read Analog X3
int volts4 = (analogRead(A4) /1001); //read Analog X4
int volts5 = (analogRead(A5) /1001); //read Analog X5
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString); // print to the serial port too:
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
Serial.println (volts0);
Serial.println (volts1);
Serial.println (volts2);
Serial.println (volts3);
Serial.println (volts4);
Serial.println (volts5);
delay (2000);
if (analogRead(A0) < ref ){ // need to compaire if high or low
Serial.println ("A0 is low ");
s0 = 0;
} else {
Serial.println ("A0 is high ");
s0 = 1;
Serial.println (s0);
}
//////
if (analogRead(A1) < ref){ // need to compaire if high or low
Serial.println ("A1 is low ");
} else {
Serial.println ("A1 is high ");
delay(200);
}
//////
if (analogRead(A2) < ref) { // need to compaire if high or low
Serial.println ("A2 is low ");
} else {
Serial.println ("A2 is high ");
delay(200);
}
//////
if (analogRead(A3) < ref){ // need to compaire if high or low
Serial.println ("A3 is low ");
} else {
Serial.println ("A3 is high");
delay(200);
}
//////
if (analogRead(A4) < ref){ // need to compaire if high or low
Serial.println ("A4 is low");
} else {
Serial.println ("A4 is high ");
delay(200);
}
/////
if (analogRead(A5) < ref){ // need to compaire if high or low
Serial.println ("A5 is low ");
} else {
Serial.println ("A5 is high ");
delay(200);
}
///////////////////////////////
delay(200);
Serial.println ("s0 is");
/////////
if (volts1 != laststatevolts1) { //(volts1 != laststatevolts1)
laststatevolts1 = volts1; // save into laststa...1
if (volts1 != 0){
counter1++;
}else{
// do nothing
}
}// if (volts1
}// loop(