Offline
Newbie
Karma: 0
Posts: 9
|
 |
« on: March 02, 2011, 02:55:33 am » |
Hi,
I've been trying to track down this problem all evening so I figured I'd ask here. I would like to use digitalread in void setup, but it doesnt begin to work (it returns 0 regardless) until after the first pass of loop(). After that, the digitalread works fine.
Heres the code, its a little messy as I am debugging, sorry!
//highswitch will need voltage divider with debouncing //phototrans needs divider and 0.1s time constant in hardware
#include <EEPROM.h>
// pin assignments const int lowbeam = 9; // lowbeam fet pin 9 const int highbeam = 10; // highbeam fet pin 10 const int highswitch = 2; // interrupts supported on digital pins 2 and 3. digital pin 2 is chip pin #4 const int phototransistor = 5; // chip pin #28
//duty cycles const int modlowduty = 50; // const int modhighduty = 200; // const int daylbduty = 100; // set to improve bulb life const int dayhbduty = 100; // const int minduty = 50; // minimum duty to complete halogen cycle const int maxduty = 255; // maximum voltage to bulbs, must measure
//constants const int filtertime = 20; //length of box filter for phototransistor, ~0.1s x filtertime
//variables int highswitchstate = 0; //first character of state (multiplied by 10), 1 = high beam selected int daylightstate = 0; //second character of state, 1 = day int state = 1; // output state int daylight = 0; // Threshold daytime ADC value. to be read given certain input upon startup unsigned int modstate = 0; int photocell = 1023; int photocellhist [filtertime]; // array to hold photocell history int histpos = 0; // position in history array unsigned int histtotal = photocell; int highswitchhist[5] = {0,0,0,0,0}; int numchanges=0;
void setup() { Serial.begin(9600); pinMode(highswitch, INPUT); // if 4 highswitch changes in 2s then read and store daylight cutoff, else read cutoff from eeprom // count number of highswitch changes in 2s highswitchhist[0] = digitalRead(highswitch); for(int i=0;i < 100; i++) { highswitchstate = digitalRead(highswitch); Serial.println("setup"); Serial.println(i); Serial.println(highswitchstate); if(highswitchstate =! highswitchhist[histpos] && histpos < 3) { histpos++; highswitchhist[histpos]=highswitchstate; } delay(20); } // count number of changes for(int i=0;i<4;i++) { if(highswitchhist!=highswitchhist[i+1]) numchanges++; } // store new daylight threshold & set daylight variable if (numchanges>3) { for (int i=0; i<filtertime; i++) { daylight = daylight + analogRead(phototransistor); delay(50); } daylight = daylight / filtertime; EEPROM.write(1, daylight / 4); //writes a byte into eeprom (0-255) } //read old daylight threshold & set photocell history to night so no modulation on startup else { daylight = 4 * EEPROM.read(1); for(int i=0;i<filtertime;i++) //initialize, test, increment { photocellhist=photocell; //set history to night so no modulation on startup at night } }
histpos = 0; highswitchstate = 0; TCCR1B = TCCR1B & 0b11111000 | 0x04 ; //set pin 9,10 pwm frequency to ~122 hz }
void loop() { highswitchstate = digitalRead(highswitch); Serial.println("nested shit"); Serial.println(digitalRead(highswitch)); // box filter photocell pin over filtertime histtotal = histtotal - photocellhist [histpos]; photocellhist[histpos] = analogRead(phototransistor); //100us to read histtotal = histtotal + photocellhist[histpos]; histpos++; if(histpos >= filtertime) histpos = 0; photocell = histtotal / filtertime;
if (photocell < daylight) daylightstate = 1; else daylightstate=0; state = 10*highswitchstate+daylightstate; switch(state) { case 11: // daytime, high visibility, modulate both beams if (modstate % 2){ analogWrite(highbeam, modlowduty); analogWrite(lowbeam, 0); } else { analogWrite(highbeam, modhighduty); analogWrite(lowbeam, 0); } Serial.println("modulating"); modstate++; delay(107); break; case 1: analogWrite(highbeam, dayhbduty); analogWrite(lowbeam, 0); delay(107); break; // daytime, low visibility case 10: analogWrite(highbeam, maxduty); analogWrite(lowbeam, 0); delay(107); break; // night, high beam case 0: analogWrite(highbeam, minduty); analogWrite(lowbeam, 0); delay(107); break; // night, low beam default: break; } }
|