Show Posts
Pages: [1] 2
1  Using Arduino / Project Guidance / Dog Door on: October 20, 2013, 05:25:25 pm
Hi all, I have made a dog door opener using sensors to detect the dog inside or outside. I want to have the dog with a collar that a antenna can read instead of the sensors. The dog is large so he can carry a couple of batteries if needed. Does anyone know what i should buy and where? I need some range between the collar and antenna for it to work properly.

Thanks
2  Using Arduino / Programming Questions / SD card writing/reading on: March 21, 2013, 04:34:33 pm
So I have my program working but I am having issues with writing to the SD card and possibly reading. I have a couter running and want to write to the SD card every 20 counts. The issue is as the count gets larger the cycle time to write to the card increases too much and I will miss a cycle (I want to be under 120milliseconds to write to it). The SD card is a class 10. Is there a better way of doing this?
Code:
if ((RaceMode > 0) && (cycles % 20 == 0) && (cycles > 1)) {
    FileWrite ();
    }
    if ((RaceMode > 1) && (cycles % 20 == 1)) {
    FileRead ();
    }
void FileWrite() {

  char fileName[16];
  sprintf(fileName, "/%d/%04d.txt", RecDir, cycles);
  //Serial.println(fileName);
  myFile1 = SD.open(fileName, FILE_WRITE);
  if (myFile1) {
  TempTime1 = millis();
  if (cycles % 40 == 0) {
  Avgspeed = (1440000*rim/(((TempTime1)-(MenuTime + StartTime))-AvgTime));
  //Serial.println(Avgspeed);
  //Serial.print(" Avgspeed");
  //Serial.println ("  ");
  AvgTime = (TempTime1)-(MenuTime + StartTime);
  //Serial.print(TempTime);
  //Serial.println(" TempTime");
  myFile1.println(Avgspeed);
  }
  else {
    Timerec = ((TempTime1 -(MenuTime + StartTime)) /100);
   //Serial.println ("  ");
   //Serial.println(Timerec);
   //Serial.println(" Time Rec");
   //Serial.print(StartTime);
   //Serial.println (" StartTime");
   //Serial.print (MenuTime);
   //Serial.println (" Total Menu Time");
   //Serial.println (" ");
   myFile1.println(Timerec);
  }
  myFile1.close();
  //Serial.println("done.");
  }
  //serialTimer = millis();
  //Serial.println ("              Write");
  Debounce = 1;
 }


 
 void FileRead() {
  //Serial.println("R");
  if (cycles == 1) {
    B = (40);
  }
  else if (cycles % 40 == 1) {
    B = (cycles + 39);
  }
 else {
   B = (cycles - 1);
 }
 //Serial.print(B);
 //Serial.println("  B");
  char AvgSR[15];
  sprintf(AvgSR, "/%d/%05d.txt", ReadDir, B);
  filedata = 0;
  File myFile1 = SD.open(AvgSR);
    while (true)
        {
        if (myFile1.available() == 0)
            break;
        digit = myFile1.read();
        if (digit == '\n' || digit == '\r')
            break;
        filedata = filedata * 10 + (digit - '0');
        }
  myFile1.close();
  if (B % 40 == 0) {
  totalavg = filedata;
  //Serial.println ("  ");
  //Serial.print (totalavg);
  //Serial.println ("  totalavg");
  //Serial.println ("  ");
  }
  else {
    timeavg = filedata;
    //Serial.println ("  ");
    //Serial.print (timeavg);
    //Serial.println ("  timeavg");
    //Serial.println ("  ");
    //TimeCompareText();
  }
  Debounce = 1;
 }
3  Using Arduino / Programming Questions / Re: sprintf on: March 19, 2013, 04:45:23 pm
Thank You!
4  Using Arduino / Programming Questions / sprintf on: March 19, 2013, 04:35:10 pm
Hi looking for some help with this code. I want to have a letter change in my sprintf code.
Code:
char C = 'S';
unsigned long G = 0;
void FileWrite() {
  G = (cycles/10);
  if (cycles > 8001) {
    C = 'F';
    G = ((cycles - 8000)/10);
  }
  if (cycles > 16001) {
    C = 'H';
    G = ((cycles - 16000)/10);
  }
 
  //Serial.println(G);
  //Serial.println(C);
  char fileName[16];
  sprintf(fileName, "/%d/%s%04d.txt", RecDir, C, G);
  Serial.println(fileName);
  myFile1 = SD.open(fileName, FILE_WRITE);
Right now I am not getting any the letter S to shown in the file name, I would like to have it change letters as my cycles get higher.

Thanks
5  Using Arduino / Programming Questions / Re: Need to reduce the size of code. Any Tips on: December 29, 2012, 06:05:11 pm
Thanks Bill, I think that will squeeze a bit more space.
6  Using Arduino / Programming Questions / Re: Need to reduce the size of code. Any Tips on: December 29, 2012, 02:41:49 pm
Code:

void TimecompareW() {
  enableSPI();
  char Times[20];
  sprintf(Times, "/Write/T%05d.txt", B+19);
  myFile1 = SD.open(Times, FILE_WRITE);
  if (myFile1) {
   serialTimer =millis();
   Timerec = ((serialTimer -(MenuTime + StartTime)) /100);
   myFile1.println(Timerec);
 
  myFile1.close();
  }
  disableSPI();
}
 
 unsigned long Speedtext() {
  if (Speed < 10 || cycles == 1)
  {A = (77);
  }
  else { A=(47);
  }
  tft.setTextColor(BLACK);
  if (Speed < ((totalavg/10)-2) && (Race == 1) && (newride == 0))
  { tft.setTextColor(RED);
  tft.fillTriangle (120, 240, 50, 200, 190, 200, (RED));
  tft.fillRect (95, 180, 50, 20, (RED));
  tft.fillRect (50, 80, 140, 60, WHITE);
   }
  else if (Speed > ((totalavg/10)+2) && (Race == 1) && (newride == 0))
  { tft.setTextColor(BLUE);
  tft.fillTriangle (120, 80, 50, 120, 190, 120, (BLUE));
  tft.fillRect (95, 120, 50, 20, (BLUE));
  tft.fillRect (50, 180, 140, 60, WHITE);
  }
  else {
    tft.fillRect (50, 180, 140, 60, WHITE);
    tft.fillRect (50, 80, 140, 60, WHITE);
  }
  tft.setCursor(A, 142);
  tft.setTextSize(5);
  tft.fillRect (47, 142, 146, 40, WHITE);
  tft.println(Speed, 1);
}

 unsigned long TimeCompareText() {
  serialTimer = millis();
  timedif = (((timeavg*100)-((serialTimer)-(MenuTime + StartTime)))/1000); //Time compare start fix
  if (timedif < 10 && timedif > 0)
  {A = (151);
  }
  else if ((timedif <= (0-10) && timedif > -100) || (timedif > 100))
  { A=(115);
  }
  else if (timedif < (0-100))
  { A=(97);
  }
  else {
    A=(133);
  }
  tft.setTextSize(2);
  tft.setCursor(10, 25);
  tft.fillRect (10, 25, 100, 30, WHITE);
  if (timedif > 0) {
  tft.setTextColor(BLUE);
  tft.println("AHEAD");
  }
  else {
  tft.setTextColor(RED);
  tft.println("BEHIND");
  }
  tft.setCursor(A, 20);
  tft.setTextSize(3);
  tft.fillRect (105, 18, 120, 30, WHITE);
  tft.println(timedif);
}

void menu1 () {
   tft.fillCircle(25, 25, 10, RED);
   tft.fillCircle(25, 75, 10, 0x57E0);
   tft.fillCircle(25, 125, 10, 0x57E0);
   tft.fillCircle(25, 175, 10, 0x57E0);
   tft.setTextColor(BLACK);
   tft.setTextSize(2);
   tft.setCursor(50, 18);
   tft.println("Course Select");
   tft.setCursor(50, 68);
   tft.println("Purge Temp Data");
   tft.setCursor(50, 118);
   tft.println("New Course");
   tft.setCursor(50, 168);
   tft.println("Exit");
   
   while ((digitalRead(Index) == LOW)&& (digitalRead(Select) == LOW));
   if (digitalRead(Index) == HIGH) {
       tft.fillCircle(25, 25, 10, 0x57E0);
       tft.fillCircle(25, 75, 10, RED);
       delay (250);
       
        while ((digitalRead(Index) == LOW)&& (digitalRead(Select) == LOW));
        if (digitalRead(Index) == HIGH) {
        tft.fillCircle(25, 75, 10, 0x57E0); 
        tft.fillCircle(25, 125, 10, RED);
        delay (250);
       
          while ((digitalRead(Index) == LOW)&& (digitalRead(Select) == LOW));
          if (digitalRead(Index) == HIGH) {
          tft.fillCircle(25, 125, 10, 0x57E0); 
          tft.fillCircle(25, 175, 10, RED);
          delay (250);
         
             while ((digitalRead(Index) == LOW)&& (digitalRead(Select) == LOW));
             if (digitalRead(Index) == HIGH) {
             delay (250);
             menu1 ();
             }
             if (digitalRead(Select) == HIGH) {
             tft.fillCircle(25, 175, 10, BLUE);
             Race =(0);
             delay (250);
             }
          }
         
          if (digitalRead(Select) == HIGH) {
          tft.fillCircle(25, 125, 10, BLUE);
          Race =(1);
          newride = (1);
          delay (250);
          }       
        }
      if (digitalRead(Select) == HIGH) {
      tft.fillCircle(25, 75, 10, BLUE);
      Race =(0);
      cycles = (0);
      delay (250);
      }     
   }
   if (digitalRead(Select) == HIGH) {
   tft.fillCircle(25, 25, 10, BLUE);
   delay (250);
   tft.fillScreen(0x62EC);
   menu2 ();
   }
}
 
void menu2 () {   
   tft.fillCircle(25, 25, 10, RED);
   tft.fillCircle(25, 75, 10, 0x57E0);
   tft.fillCircle(25, 125, 10, 0x57E0);
   tft.fillCircle(25, 175, 10, 0x57E0);
   tft.setTextColor(BLACK);
   tft.setTextSize(2);
   tft.setCursor(50, 18);
   tft.println("Fanshawe CW");
   tft.setCursor(50, 68);
   tft.println("Fanshawe CCW");
   tft.setCursor(50, 118);
   tft.println("Other");
   tft.setCursor(50, 168);
   tft.println("Exit");
   
   while ((digitalRead(Index) == LOW)&& (digitalRead(Select) == LOW));
   if (digitalRead(Index) == HIGH) {
       tft.fillCircle(25, 25, 10, 0x57E0);
       tft.fillCircle(25, 75, 10, RED);
       delay (250);
       
        while ((digitalRead(Index) == LOW)&& (digitalRead(Select) == LOW));
        if (digitalRead(Index) == HIGH) {
        tft.fillCircle(25, 75, 10, 0x57E0); 
        tft.fillCircle(25, 125, 10, RED);
        delay (250);
       
          while ((digitalRead(Index) == LOW)&& (digitalRead(Select) == LOW));
          if (digitalRead(Index) == HIGH) {
          tft.fillCircle(25, 125, 10, 0x57E0); 
          tft.fillCircle(25, 175, 10, RED);
          delay (250);
         
             while ((digitalRead(Index) == LOW)&& (digitalRead(Select) == LOW));
             if (digitalRead(Index) == HIGH) {
             delay (250);
             menu2 ();
             }
             if (digitalRead(Select) == HIGH) {
             tft.fillCircle(25, 175, 10, BLUE);
             delay (250);
             }
          }
         
          if (digitalRead(Select) == HIGH) {
          tft.fillCircle(25, 125, 10, BLUE);
          delay (250);
          }       
        }
      if (digitalRead(Select) == HIGH) {
      tft.fillCircle(25, 75, 10, BLUE);
      delay (250);
      }     
   }
   if (digitalRead(Select) == HIGH) {
   tft.fillCircle(25, 25, 10, BLUE);
   Race =(1);
   cycles = (0);
   speedcycles = (0);
   delay (250);
   }
}

void menu3 () { 
   tft.fillCircle(25, 25, 10, RED);
   tft.fillCircle(25, 75, 10, 0x57E0);
   tft.setTextColor(BLACK);
   tft.setTextSize(2);
   tft.setCursor(50, 18);
   tft.println("Resume");
   tft.setCursor(50, 68);
   tft.println("Cancel Ride");
   
   while ((digitalRead(Index) == LOW)&& (digitalRead(Select) == LOW));
   if (digitalRead(Index) == HIGH) {
       tft.fillCircle(25, 25, 10, 0x57E0);
       tft.fillCircle(25, 75, 10, RED);
       delay (250);
       
        while ((digitalRead(Index) == LOW)&& (digitalRead(Select) == LOW));
        if (digitalRead(Index) == HIGH) {
        delay (250);
        menu3 ();
        }
        if (digitalRead(Select) == HIGH) {
        tft.fillCircle(25, 75, 10, BLUE);
        cycles = (0);
        Race = (0);
        StartTime = (0);
        MenuTime = (0);
        MenuBegin = (0);
        delay (250);
        }
   }
   if (digitalRead(Select) == HIGH) {
   tft.fillCircle(25, 25, 10, BLUE);
   
   delay (250);
   }
}


void menu (){
  delay (250);
  tft.fillScreen(0x62EC);
  if (Race == 0) {
  menu1 ();
  }
  else {
  menu3 ();
  }
  delay (250);
  Background();
}
7  Using Arduino / Programming Questions / Need to reduce the size of code. Any Tips on: December 29, 2012, 02:40:52 pm
I am looking into using switch case for the menu, but hoping there are some other tips that can reduce the size. I need to upload the code in 2 parts due to size.
Code:
#include <Adafruit_GFX.h>
#include <Adafruit_TFTLCD.h>
#include <SD.h>
#define SD_CS 5
#define LCD_RESET A4
#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
//#define CYAN    0x07FF
//#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

Adafruit_TFTLCD tft;
int8_t saved_spimode;

void disableSPI(void) {
  saved_spimode = SPCR;
  SPCR = 0;
}

void enableSPI(void) {
  SPCR = saved_spimode;
}

const int buttonPin = 2;
const int Index = A4;
const int Select = A5;
float rim = 2.110;
int cycles = 0;
int speedcycles = 0;
unsigned long revtimer;
unsigned long revtimer2;
unsigned long serialTimer;
float Speed = 0;
unsigned long AvgTime2 = 0;
unsigned long AvgTime = 0;
unsigned long TempTime = 0;
unsigned long MenuTime = 0;
unsigned long MenuBegin = 0;
unsigned long StartTime = 0;
unsigned long Timerec = 0;
int Avgspeed =0;
float timedif = 0;
byte A = 10;
byte newride = 0;
byte Race = 0;
int Debounce = 0;
int B = 0;
int C = 0;
File myFile1;
//File myFile2;
//File myFile3;
//File myFile4;
int digit;
float totalavg = 0;
float timeavg = 0;

void setup()
{
  Serial.begin (9600);
  pinMode (buttonPin, INPUT);
  pinMode (Index, INPUT);
  pinMode (Select, INPUT);
  tft.reset();
  uint16_t identifier = tft.readID();
  tft.begin(identifier);
  if (!SD.begin(SD_CS)) {
    return;
  }
  disableSPI();
  Background();
}

void loop ()
{
  while ((digitalRead(Index) == LOW)&& (digitalRead(buttonPin) == LOW));
  if (digitalRead(Index) == HIGH){
    if (Race == 1) {
    MenuBegin = millis();
    }
    menu ();
    if ((Race == 1) && (MenuBegin > 0)) {
    serialTimer =millis();
    MenuBegin = (serialTimer)-(MenuBegin);
    MenuTime = (MenuTime) + (MenuBegin);
    }
  }
  if (digitalRead(buttonPin) == HIGH){ 
    if ((cycles == 0) && (Race == 1)) {
      StartTime = millis();
    }
    if (Race == 1) {
    cycles++;
    }
   
  while (digitalRead(buttonPin) == HIGH);
  speedcycles++;
  if (speedcycles % 4 == 0) {
  Serial.println (speedcycles);
  Currentspeed ();
  Speedtext(); 
  }
  Debounce = 0;
  for (B=1; B<26000; B+=40){
  if (cycles == B+39) {
  AvgspeedW();
  }
  if (cycles == B+19) {
  TimecompareW();
  }
  if ((cycles == B) && (newride==0)) {
  AvgspeedR();
  Debounce = 1;
  }
  if ((cycles == B+20) && (newride==0)) {
  TimeR();
  Debounce = 1;
  }
  if ((cycles == B+21) && (newride==0)) {
  TimeCompareText();
  }
  }
   if (Debounce == 0) {
    if (Speed < 40) {
    Debounce = (map (Speed, 1, 40, 240, 1));
    }
  delay (Debounce);
  delay (60);
  }
  } 
}

unsigned long Background() {
  tft.fillScreen(WHITE);
}

void Currentspeed () {
   revtimer = millis();
   Speed = ((rim * 14400)/(revtimer - revtimer2));
   revtimer2 = (revtimer);
}

void AvgspeedR() {
  enableSPI();
  char AvgSR[19];
  sprintf(AvgSR, "/Read/S%05d.txt", B+39);
  totalavg = 0;
  File myFile1 = SD.open(AvgSR);
    while (true)
        {
        if (myFile1.available() == 0)
            break;
        digit = myFile1.read();
        if (digit == '\n' || digit == '\r')
            break;
        totalavg = totalavg * 10 + (digit - '0');
        }
  myFile1.close();
  disableSPI();
 }
 
void TimeR() {
  enableSPI();
  char TimeSR[19];
  sprintf(TimeSR, "/Read/T%05d.txt", B+19);
  timeavg = 0;
  File myFile1 = SD.open(TimeSR);
    while (true)
        {
        if (myFile1.available() == 0)
            break;
        digit = myFile1.read();
        if (digit == '\n' || digit == '\r')
            break;
        timeavg = timeavg * 10 + (digit - '0');
        }
  myFile1.close();
  disableSPI();
 }

void AvgspeedW() {
  enableSPI();
  char fileName[20];
  sprintf(fileName, "/Write/S%05d.txt", B+39);
  myFile1 = SD.open(fileName, FILE_WRITE);
  if (myFile1) {
  serialTimer = millis();
  AvgTime2 = (serialTimer)-(MenuTime + StartTime);
  Avgspeed = (1440000*rim/(AvgTime2-TempTime));
  TempTime = (AvgTime2);
  myFile1.println(Avgspeed);
  myFile1.close();
  }
  disableSPI();
 }
8  Using Arduino / Programming Questions / Re: Menu using 2 Buttons on: November 25, 2012, 06:39:41 pm
Thanks for the help I changed the code so its working now.
Code:
while ((digitalRead(Index) == LOW)&& (digitalRead(Select) == LOW) );
    if (digitalRead(Index) == HIGH) {
      tft.fillCircle(25, 25, 10, RED);
    }
    if (digitalRead(Select) == HIGH) {
      tft.fillCircle(25, 75, 10, RED);
    }
9  Using Arduino / Programming Questions / Re: Menu using 2 Buttons on: November 25, 2012, 05:17:22 pm
So how should this code look to do what i want it to.
10  Using Arduino / Programming Questions / Re: Menu using 2 Buttons on: November 25, 2012, 03:07:57 pm
Thre Problem is I can't seem to make the "if" commands turn the circles red when either button is pushed in the menu code.
Code:
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <SD.h>

#define SD_CS 5

#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

Adafruit_TFTLCD tft;
int8_t saved_spimode;

void disableSPI(void) {
  saved_spimode = SPCR;
  SPCR = 0;
}

void enableSPI(void) {
  SPCR = saved_spimode;
}

const int buttonPin = 2;
const int Index = A4;
const int Select = A5;
//int sensorPin = A4;
float Matchspeed = 0.0;
float rim = 2.170;
int cycles = 0;
unsigned long revtimer;
unsigned long revtimer2;
unsigned long serialTimer;
float Speed = 0;
float distance = 0;

unsigned long AvgTime2 = 0;
unsigned long AvgTime = 0;
unsigned long TempTime = 0;
int Avgspeed =0;
int cyclecount = 0;
float speeddif = 0;
float timedif = 0;
byte A = 10;
int B = 0;
File myFile1;
File myFile2;
File myFile3;
File myFile4;
int digit;
float totalavg = 0;
float timeavg = 0;

void setup()
{
  Serial.begin (9600);
  pinMode (buttonPin, INPUT);
  pinMode (Index, INPUT);
  pinMode (Select, INPUT);
  tft.reset();
  uint16_t identifier = tft.readID();
  tft.begin(identifier);
 
  Serial.print("Initializing SD card...");
  if (!SD.begin(SD_CS)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  disableSPI();
  Background();
  serialTimer =millis();
}


void loop ()
{
 
  if (digitalRead(Index) == HIGH){
  menu ();
  }
  if (digitalRead(buttonPin) == HIGH){
   
  cycles++;
  while (digitalRead(buttonPin) == HIGH);
  //while (digitalRead(buttonPin) == LOW);
  delay (10);
  Currentspeed ();
  TimeCompareText();
  sendstats();
  Speedtext();
 
  for (B=1; B<26000; B+=40){
  //if (cycles == B+39)
  //AvgspeedW(); 
  //if (cycles == B+19)
  //TimecompareW();
  if (cycles == B)
  AvgspeedR();
  if (cycles == B+20)
  TimeR();
  //if (cycles == B+21)
  //TimeCompareText();
  }
  } 
}


unsigned long Background() {
  tft.fillScreen(WHITE);
  tft.fillRect (0, 60, 240, 5, BLACK);
  tft.fillRect (0, 260, 240, 5, BLACK);
  //tft.setCursor(160, 190);
  //tft.setTextColor(BLACK);  tft.setTextSize(2);
  //tft.println("km/hr");
  //tft.setCursor(160, 110);
  //tft.setTextColor(BLACK);  tft.setTextSize(2);
  //tft.println("km/hr");
}

void Currentspeed () {
   serialTimer = millis();
   revtimer = (serialTimer);
   //Serial.print(revtimer);
   //Serial.println (" revtimer");
   Speed = ((rim * 3600)/(revtimer - revtimer2));
   //Serial.print(Speed);
   //Serial.println (" Speed");
   revtimer2 = (serialTimer);
   //Serial.print(revtimer2);
   //Serial.println (" revtimer2");
}

void AvgspeedR() {
  enableSPI();
  char AvgSR[27];
  sprintf(AvgSR, "/Read/Speed/speed%03d.txt", B+39);
  totalavg = 0;
  File myFile1 = SD.open(AvgSR);
    while (true)
        {
        if (myFile1.available() == 0)
            break;
        digit = myFile1.read();
        if (digit == '\n' || digit == '\r')
            break;
        totalavg = totalavg * 10 + (digit - '0');
        }
  Serial.println (totalavg);
  myFile1.close();
  disableSPI();
 }
 
void TimeR() {
  enableSPI();
  char TimeSR[26];
  sprintf(TimeSR, "/Read/Times/time%03d.txt", B+19);
  timeavg = 0;
  File myFile4 = SD.open(TimeSR);
    while (true)
        {
        if (myFile4.available() == 0)
            break;
        digit = myFile4.read();
        if (digit == '\n' || digit == '\r')
            break;
        timeavg = timeavg * 10 + (digit - '0');
        }
  Serial.println (timeavg);
  myFile4.close();
  disableSPI();
 }


void AvgspeedW() {
  enableSPI();
  char fileName[28];
  sprintf(fileName, "/Write/Speed/speed%03d.txt", B+39);
  myFile2 = SD.open(fileName, FILE_WRITE);
  if (myFile2) {
  AvgTime2 = (serialTimer);
  Avgspeed = (1800000*rim/(AvgTime2-TempTime));
  TempTime = (serialTimer);
  myFile2.println(Avgspeed);
  myFile2.close();
  Serial.println("done.");
  }
  disableSPI();
 }

 
void TimecompareW() {
  enableSPI();
  char Times[27];
  sprintf(Times, "/Write/Times/time%03d.txt", B+19);
  myFile3 = SD.open(Times, FILE_WRITE);
  if (myFile3) {
  myFile3.println(serialTimer/100);
  myFile3.close();
  Serial.println("done.");
  }
  disableSPI();
}
 
 
 
  void sendstats() {
 // Serial.print (Speed);
 // Serial.println (" km/hr");
 // serialTimer = millis();
 // Serial.print (serialTimer/1000);
 // Serial.println (" s Ride Time");
 // Serial.println (distance);
  Serial.println (cycles);
 // Serial.println (serialTimer);
  Serial.println (" ");
 }
 
 unsigned long Speedtext() {
  if (Speed < 10 || cycles == 1)
  {A = (77);
  }
  else { A=(47);
  }
  tft.setTextColor(BLACK);
  if (Speed < ((totalavg/10)-2))
  { tft.setTextColor(RED);
  tft.fillTriangle (120, 240, 50, 200, 190, 200, (RED));
  tft.fillRect (95, 180, 50, 20, (RED)); // x,y , x distance, y distance
  tft.fillRect (50, 80, 140, 60, WHITE);
   }
  else if (Speed > ((totalavg/10)+2))
  { tft.setTextColor(BLUE);
  tft.fillTriangle (120, 80, 50, 120, 190, 120, (BLUE));
  tft.fillRect (95, 120, 50, 20, (BLUE)); // x,y , x distance, y distance
  tft.fillTriangle (115, 90, 112, 109, 121, 107, (YELLOW));
  tft.fillTriangle (115, 90, 127, 87, 121, 107, (YELLOW));
  tft.fillTriangle (122, 103, 134, 101, 110, 136, (YELLOW));
  tft.fillRect (50, 180, 140, 60, WHITE);
  }
  else {
    tft.fillRect (50, 180, 140, 60, WHITE);
    tft.fillRect (50, 80, 140, 60, WHITE);
  }
  tft.setCursor(A, 142);
  tft.setTextSize(5);
  tft.fillRect (47, 142, 146, 40, WHITE);
  tft.println(Speed);
}


 unsigned long TimeCompareText() {
  serialTimer = millis();
  timedif = (((timeavg*100)-(serialTimer))/1000);
  if (timedif < 10 && timedif > 0)
  {A = (151);
  tft.setTextColor(BLUE);
  }
  else if (timedif > 10)
  { A=(133);
  tft.setTextColor(BLUE);
  }
  else if (timedif <= (0-10) && timedif > -100)
  { A=(115);
  tft.setTextColor(RED);
  }
  else if (timedif < (0-100))
  { A=(97);
  tft.setTextColor(RED);
  }
  else if (timedif > 100)
  { A=(115);
  tft.setTextColor(BLUE);
  }
  else {
    A=(133);
    tft.setTextColor(RED);
  }
  if (timedif > 0) {
  tft.setCursor(10, 25);
  tft.setTextColor(BLUE);  tft.setTextSize(2);
  tft.fillRect (10, 25, 100, 30, WHITE);
  tft.println("AHEAD");
  }
  else {
  tft.setCursor(10, 25);
  tft.setTextColor(RED);  tft.setTextSize(2);
  tft.fillRect (10, 25, 100, 30, WHITE);
  tft.println("BEHIND");
  }
  tft.setCursor(A, 20);
  tft.setTextSize(3);
  tft.fillRect (105, 18, 120, 30, WHITE);
  tft.println(timedif); 
}

void menu (){
  delay (500);
  tft.fillScreen(0x62EC);
  tft.fillCircle(25, 25, 10, 0x57E0);
  tft.fillCircle(25, 75, 10, 0x57E0);
  tft.fillCircle(25, 125, 10, 0x57E0);
  //if (cycles==4) {
  tft.setCursor(50, 18);
  tft.setTextColor(BLACK);  tft.setTextSize(2);
  tft.println("RIDE SETTINGS");
  tft.setCursor(50, 68);
  tft.println("testing A");
  tft.setCursor(50, 118);
  tft.println("testing B");
  while ((digitalRead(Index) == HIGH)|| (digitalRead(Select) == HIGH) );
    if (digitalRead(Index) == HIGH) {
      tft.fillCircle(25, 25, 10, RED);
    }
    if (digitalRead(Select) == HIGH) {
      tft.fillCircle(25, 75, 10, RED);
    }
   
  delay (5000);
  Background();
  return ;
}
11  Using Arduino / Programming Questions / Menu using 2 Buttons on: November 25, 2012, 01:15:45 pm
I am trying to setup a menu but am having some trouble. If i push the Index button I can bring up the menu. I then want to be able to push either the index or select button to do whatever task. Please have a look at the code and let me know what I should do.
Thanks
Code:
void menu (){
  delay (500);
  tft.fillScreen(0x62EC);
  tft.fillCircle(25, 25, 10, 0x57E0);
  tft.fillCircle(25, 75, 10, 0x57E0);
  tft.fillCircle(25, 125, 10, 0x57E0);
  tft.setCursor(50, 18);
  tft.setTextColor(BLACK);  tft.setTextSize(2);
  tft.println("RIDE SETTINGS");
  tft.setCursor(50, 68);
  tft.println("testing A");
  tft.setCursor(50, 118);
  tft.println("testing B");
  while ((digitalRead(Index) == HIGH)|| (digitalRead(Select) == HIGH) );
    if (digitalRead(Index) == HIGH) {
      tft.fillCircle(25, 25, 10, RED);
    }
    if (digitalRead(Select) == HIGH) {
      tft.fillCircle(25, 75, 10, RED);
    }
    if (digitalRead(Select) == LOW) {
      tft.fillCircle(25, 125, 10, RED);
    }
  delay (5000);
  Background();
  return ;
}
12  Using Arduino / Programming Questions / Re: Reading a txt file on: November 17, 2012, 10:21:48 pm
Thank you, this works. I will have to spend some time figuring out how it works next.
13  Using Arduino / Programming Questions / Reading a txt file on: November 17, 2012, 08:53:35 pm
Ok so I am trying to read a txt file from the SD card (the file was written from the arduino and moved to a different directory) I am using the code below to convert the # but I can see I am getting Line feed and Carriage return #'s(-35, -38). How should I correct this? The # should be 259
Thank you for your help.

Reading Speed File
10000.00
2.00
5.00
9.00
-35.00
-38.00
25512.00
0.00
Code:
void AvgspeedR() {
  enableSPI();
  char AvgSR[27];
  sprintf(AvgSR, "/Read/Speed/speed%03d.txt", B+39);
  File myFile1 = SD.open(AvgSR);
  if (myFile1)
  {
    Serial.println("Reading Speed File");
    float decade = pow(10, (myFile1.available() - 1));
    Serial.println (decade);
    while(myFile1.available())
   {
     float temp3 = (myFile1.read() - '0');
     Serial.println (temp3);
     Matchspeed = temp3*decade+Matchspeed;
     decade = decade/10;
    }
  Serial.println (Matchspeed);
  Matchspeed = 0;
  Serial.println (Matchspeed);
  myFile2.close();
  }
  disableSPI();
 }
14  Using Arduino / Programming Questions / Re: Reading integers from SD card on: November 15, 2012, 11:57:09 am
Thank you Dan! That works just how i want.
15  Using Arduino / Programming Questions / Re: Reading integers from SD card on: November 15, 2012, 10:13:09 am
Ok I know this code is a mess. But can someone shown me how it should look. Not sure how to arrange sprintf.
Code:
void test ()
{
enableSPI();
   char D = (B); //B is a counter
   File newfile;
   File myFile2;
   myFile2 = SD.open((sprintf (newfile, "sensor%d.txt", (D) )) , FILE_WRITE); //know idea of how to arrange this, do I use D or B?
   if (myFile2) {
   Serial.print("Writing to test.txt...");
   myFile2.println(B);
   myFile2.close();
   Serial.println("done.");
  } else {
    Serial.println("error opening test.txt");
  }
  disableSPI();  
}
Pages: [1] 2