Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

arduino issues

By: a guest on Feb 2nd, 2013  |  syntax: C  |  size: 5.96 KB  |  views: 43  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //copyright Piers Walter. Do not distribute without permission.
  2.  
  3. #include <SPI.h>
  4. //include LCD library
  5. #include <LiquidCrystal.h>
  6. LiquidCrystal lcd(2,3,4,5,6,8);//LCD pins, intialize
  7.  
  8. char* day[7] = {"MON ","TUE ","WED ","THU ","FRI ","SAT ","SUN "}; //set char string, remember that to print monday would be lcd.print(day[0])
  9.  
  10. unsigned long oldmillisec;
  11.  
  12. int setBtn = A2;
  13. int downBtn = A2;
  14. int upBtn = A2;
  15. int exitBtn = A2;
  16.  
  17. int volume = 0;
  18. int volumeMapped = 0;
  19.  
  20. const int slaveSelectPin = 7;
  21.  
  22. int light =0;
  23. int screenlight =0;
  24. int scrLed =10;
  25.  
  26. /*byte degree[8] = {
  27.   B00110,
  28.   B01001,
  29.   B01001,
  30.   B00110,
  31.   B00000,
  32.   B00000,
  33.   B00000,
  34. };*/
  35.  
  36. long msecPerHour=0;//hourly increment
  37. long msecPerMinute=0;//minute inrement
  38. long msecPerDay=0;//daily increment
  39. long totalSecPerDay=300;//dependent on crystal
  40. int DAY, HOURS, MINS, SECS;
  41. unsigned int MILLISECS;
  42. unsigned int currentSECS,lastSECS;
  43.  
  44. //int temp;
  45. //int temp_address = 72;
  46. int zero = 0;
  47.  
  48. void setup()
  49. {
  50.  
  51.   lcd.begin(16,2);//LCD width by height
  52.   lcd.print("Initialized");
  53.   oldmillisec=millis();
  54.   pinMode(setBtn,INPUT);
  55.   pinMode(downBtn,INPUT);
  56.   pinMode(upBtn,INPUT);
  57.   pinMode(exitBtn,INPUT);
  58.   pinMode(slaveSelectPin,OUTPUT);
  59.   SPI.begin();
  60.   SPI.setDataMode(0);
  61.  
  62. }
  63. void loop()
  64. {
  65.  
  66.   boolean hourState=false;
  67.   boolean minuteState=false;
  68.   boolean dayState=false;
  69.   boolean msecminState=false;
  70.  
  71.   while(digitalRead(7)==HIGH){
  72.     hourState = true;}
  73.   while(hourState){
  74.     lcd.setCursor(0,0);
  75.     lcd.print("Set Hours      ");
  76.     lcd.setCursor(6,1);
  77.     if(HOURS<10)lcd.print("0");
  78.     lcd.print(HOURS);
  79.     if(digitalRead(upBtn)==HIGH){HOURS++;tone(6,2500,15);}
  80.     if(digitalRead(downBtn)==HIGH){HOURS--;tone(6,2500,15);}
  81.     if(HOURS>23) HOURS -= 24;
  82.     if(HOURS<0) HOURS+=24;
  83.     delay(200);
  84.     if(digitalRead(exitBtn)==HIGH){hourState=false;
  85.     clear();}
  86.     if(digitalRead(setBtn)==HIGH){minuteState=true;
  87.       hourState=false;
  88.       delay(200);}}
  89.   while(minuteState==true){
  90.     lcd.setCursor(0,0);
  91.     lcd.print("Set Minutes    ");
  92.     lcd.setCursor(9,1);
  93.     if(MINS<10)lcd.print("0");
  94.     lcd.print(MINS);
  95.     if(digitalRead(upBtn)==HIGH)MINS++;
  96.     if(digitalRead(downBtn)==HIGH)MINS--;
  97.     if(MINS>59) MINS -= 60;
  98.     if(MINS<0) MINS+=60;
  99.     delay(100);
  100.     if(digitalRead(exitBtn)==HIGH){minuteState=false;
  101.       clear();}
  102.     if(digitalRead(setBtn)==HIGH){dayState=true;break;
  103.       delay(200);}
  104.     }
  105.    
  106.   while(dayState){
  107.     lcd.setCursor(0,0);
  108.     lcd.print("Set Days       ");
  109.     lcd.setCursor(2,1);
  110.     lcd.print(day[DAY]);
  111.     if(digitalRead(upBtn)==HIGH){DAY++;tone(6,2500,15);}
  112.     if(digitalRead(downBtn)==HIGH){DAY--;tone(6,2500,15);}
  113.     if(DAY>6) DAY -= 7;
  114.     if(DAY<0) DAY += 7;
  115.     delay(100);
  116.     if(digitalRead(exitBtn)==HIGH){dayState=false;
  117.       clear();}
  118.     if(digitalRead(setBtn)==HIGH){msecminState=true;dayState=false;}}
  119.      
  120.   while(msecminState==true){
  121.     lcd.setCursor(0,0);
  122.     lcd.print("secs/day add  ");
  123.     if(digitalRead(upBtn)==HIGH){totalSecPerDay = totalSecPerDay++;tone(6,2500,15);}
  124.     if(digitalRead(downBtn)==HIGH){totalSecPerDay = totalSecPerDay--;tone(6,2500,15);}
  125.     long totalMsecPerDay = totalSecPerDay * 1000;
  126.     msecPerMinute = totalMsecPerDay/1440;
  127.     totalMsecPerDay = totalMsecPerDay % 1440;//remainder needs to be distributed hourly
  128.     msecPerHour=totalMsecPerDay/24;
  129.     totalMsecPerDay=totalMsecPerDay%24;//remainder needs to be distributed daily.
  130.     msecPerDay=totalMsecPerDay;    
  131.     lcd.setCursor(0,1);
  132.     lcd.print("                ");
  133.     delay(10);
  134.     lcd.setCursor(0,1);
  135.     lcd.print(totalSecPerDay);
  136.     delay(50);
  137.     if(digitalRead(setBtn)==HIGH){
  138.       clear();
  139.       }
  140.     if(digitalRead(exitBtn)==HIGH){
  141.       msecminState=false;
  142.       lcd.print("  ");
  143.       lcd.print(day[DAY]);
  144.       if(HOURS<10){ lcd.print("0");}
  145.       lcd.print(HOURS);
  146.       lcd.print(":");
  147.       if(MINS<10){lcd.print("0");}
  148.       lcd.print(MINS);
  149.       lcd.print(":");
  150.       if(SECS<10){lcd.print("0");}
  151.       lcd.print(SECS);}}
  152.    
  153.   unsigned long msec = millis();
  154.   unsigned long elapsed = msec - oldmillisec;
  155.   oldmillisec = msec;//remember where we are
  156.   MILLISECS += elapsed; //increase millisecs by elapsed amount
  157.   currentSECS=SECS;
  158.   while(MILLISECS>=1000){
  159.     MILLISECS -= 1000; SECS +=1;}
  160.   if(currentSECS!=SECS){digitalWrite(13,HIGH);
  161.     delay(20);
  162.     clear();
  163.     }
  164.   if(digitalRead(exitBtn)==HIGH)SECS=0;
  165.    
  166.   while(SECS>=60){SECS -=60; MINS++;
  167.     MILLISECS+=msecPerMinute;}//add msecminute correction to elapsed
  168.   while(MINS>=60){MINS-=60; HOURS++; MILLISECS += msecPerHour;tone(6,2500,15);delay(120);tone(6,2500,15);}
  169.   while(HOURS>=24){HOURS-=24; DAY++; MILLISECS += msecPerDay;}
  170.   while(DAY>=7){DAY-=7;}
  171.  
  172.   if(digitalRead(A0)==HIGH){volume ++;
  173.     delay(10);}
  174.  
  175.   if(digitalRead(A1)==HIGH){volume --;
  176.     delay(10);}
  177.  
  178.   if(volume>100){volume=100;}
  179.   if(volume<0){volume=0;}
  180.  
  181.   volumeMapped = map(volume,0,100,0,127);
  182.  
  183.   digitalWrite(slaveSelectPin,LOW);
  184.   SPI.transfer(0);
  185.   SPI.transfer(volumeMapped);
  186.   digitalWrite(slaveSelectPin,HIGH);
  187.  
  188.   lcd.setCursor(0,0);
  189.   lcd.print("   ");
  190.   lcd.print("Volume");
  191.   lcd.print("=");
  192.   lcd.print(volume);
  193.   lcd.print("%");
  194.   lcd.setCursor(0,1);
  195.   lcd.print("  ");
  196.   lcd.print(day[DAY]);
  197.   if(HOURS<10){ lcd.print("0");}
  198.   lcd.print(HOURS);
  199.   lcd.print(":");
  200.   if(MINS<10){lcd.print("0");}
  201.   lcd.print(MINS);
  202.   lcd.print(":");
  203.   if(SECS<10){lcd.print("0");}
  204.   lcd.print(SECS);
  205.   delay(100);
  206. }
  207.  
  208. void clear(){lcd.setCursor(0,0);
  209.       lcd.print("                ");
  210.       lcd.setCursor(0,0);
  211.       lcd.setCursor(0,1);
  212.       lcd.print("                ");
  213.       delay(10);}
  214.      
  215. void digitalPotWrite(int value)
  216. {
  217.   //take the SS pin low to select the chip:
  218.   digitalWrite(slaveSelectPin, LOW);
  219.   //send in the address and value via SPI:
  220.   SPI.transfer(0);
  221.   SPI.transfer(value);
  222.   // take the SS pin high to de-select the chip:
  223.   digitalWrite(slaveSelectPin, HIGH);
  224. }
clone this paste RAW Paste Data