Go Down

Topic: digitalread not working until first pass of loop() ?! (Read 754 times) previous topic - next topic

mullenc525

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;
    }
}

CrossRoads

Your listing mentions voltage divider & debouncing - what is hooked up in the meantime?
Try enabling the internal pullup until the rest is added:

pinMode(highswitch, INPUT);
digitalWrite(highswitch, HIGH); // enables internal pullup
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

mullenc525

Currently I 5v hooked to a resistor hooked to pin 2, to go low I was grounding pin 2.

Enabling the internal pullup works, not sure why since it was already pulled up externally!?

PaulS

Quote
Currently I 5v hooked to a resistor hooked to pin 2, to go low I was grounding pin 2.

Most of my resistors have 2 legs. I presume yours do, too, but you only tell us where one leg is connected - to pin 2. Where is the other leg connected?

mullenc525

5V-resistor-Gnd. Come to think of it, it was AREF not 5V. On my breadboard those are one and the same but that may not be the case on the uno?

MarkT

The Aref pin voltage depends on the selected analog reference and I think is fairly high impedance.  Its not for connecting pullups to!!
[ I won't respond to messages, use the forum please ]

CrossRoads

Aref is Not connected to +5v on the UNO, it only has a decoupling cap to ground.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Go Up