Ejemplo n.º 1
0
int setSPD(int spd, unsigned long curTime) 
{
  /*
  This function will set the speed of the servos. It takes spd argument in deg/sec (-300, 300), and uses an aproximated
   calibration curve to set the pulse width. Sine the servos require some time to adjust the speed there is 
   lag check that will immedately return if there wasn't enough time between calls to the function
   */

  unsigned long dur = curTime - spdTime;
  
  if ( dur < SPD_WRITE_WAIT){ // return -1 if we haven't passed through the time limit, HOW do we prevent this from overflowing?
    return -1;
  } 
  spdTime = curTime; // Setting speed, so update clock

  /*
 This is an aproximated calibration curve, was built by hand using the servocalibrate degree sketch
   */
  if ( spd > 300){
    pos = 1700;
  }
  else if ( 270 < spd && spd <= 300){
    pos =  mapRange(spd, 270 , 300, 1600, 1700) + 0.5; 
  } 
  else if ( 180 < spd && spd <= 270) {
    pos =  mapRange(spd, 180, 270, 1550, 1600)+ 0.5; 
  } 
  else if ( 90 < spd && spd <= 180) {
    pos =  mapRange(spd, 90, 180, 1526, 1550)+ 0.5; 
  } 
  else if ( 1 <= spd && spd <= 90) {
    pos =  mapRange(spd, 1 , 90, 1501, 1526)+ 0.5; 
  } 
  else if ( 0 == spd) {
    pos =  1500; 
  } 
  else if ( -90 < spd && spd <= -1) {
    pos =  mapRange(spd, -90 , -1, 1471, 1494)+ 0.5; 
  } 
  else if ( -180 < spd && spd <= -90) {
    pos =  mapRange(spd, -180 , -90, 1448, 1471)+ 0.5; 
  } 
  else if ( -270 < spd && spd <= -180) {
    pos =  mapRange(spd, -270 , -180, 1400, 1448)+ 0.5; 
  } 
  else if ( -300 <= spd && spd <= -270) {
    pos =  mapRange(spd, -300 , -270, 1300, 1400)+ 0.5; 
  }
  else if (spd < -300 ) { 
    pos = 1300;
  }

  servo1.writeMicroseconds(pos);                 // Actully setting the speed.
  rpos = mapRange(pos, 1300, 1700, 1700, 1300);   //Reverse the driection for the other wheel
  servo2.writeMicroseconds(rpos);
  return pos; // otherwise return the pulse width value that came from the calibration curve.
}
Ejemplo n.º 2
0
	void FluidDrawerBase::drawVectors(float x, float y, float renderWidth, float renderHeight) {
		if(enabled == false) return;
		
		int fw = _fluidSolver->getWidth();
		int fh = _fluidSolver->getHeight();
		
		if(useAdditiveBlending) {
			glBlendFunc(GL_ONE, GL_ONE);
			glEnable(GL_BLEND);
		} else {
			glDisable(GL_BLEND);
		}
		
		//	int xStep = renderWidth / 10;		// every 10 pixels
		//	int yStep = renderHeight / 10;		// every 10 pixels
		
		glPushMatrix();
		glTranslatef(x, y, 0);
		glScalef(renderWidth/(fw-2), renderHeight/(fh-2), 1.0);
		
		float velMult = 50000;
		float maxVel = 5.0f/20000;
		
		Vec2f vel;
		float vt = velThreshold * _fluidSolver->getInvWidth() * _fluidSolver->getInvHeight();
		vt *= vt;
		
		for (int j=0; j<fh-2; j++ ){
			for (int i=0; i<fw-2; i++ ){
				_fluidSolver->getInfoAtCell(i+1, j+1, &vel, NULL);
				float d2 = vel.x * vel.x + vel.y * vel.y;
				if(d2>vt) {
					if(d2 > maxVel * maxVel) {
						float mult = maxVel * maxVel/ d2;
						//				float mult = (d2 - maxVel * maxVel) / d2;
						vel.x *= mult;
						vel.y *= mult;
					}
					vel *= velMult;
					
#ifndef MSA_TARGET_OPENGLES
					float b = mapRange(d2, vt, maxVel, 0.0f, brightness);
					b = brightness;
					glColor3f(b, b, b);
					
					glBegin(GL_LINES);
					glVertex2f(i, j);
					glVertex2f(i + vel.x, j + vel.y);
					glEnd();
#endif
				}
			}
		}
		glPopMatrix();
		
	}
Ejemplo n.º 3
0
STDMETHODIMP ThwOverlayControlVMR9::set(int cap, int val)
{
    if (!vmr9) {
        return E_UNEXPECTED;
    }
    if (!isIn(cap, 1, 6) || !caps[cap]) {
        return E_INVALIDARG;
    }
    ctrl.*(caps[cap]) = mapRange(val, std::make_pair(hwocMinMax[cap][0], hwocMinMax[cap][1]), std::make_pair(ranges[cap].MinValue, ranges[cap].MaxValue));
    vmr9->SetProcAmpControl(id, &ctrl);
    return S_OK;
}
Ejemplo n.º 4
0
 void makeTexture(){
   texture = Texture(256, 1, Graphics::LUMINANCE_ALPHA, Graphics::UBYTE, true);
   Array& sprite(texture.array());
   struct{
     uint8_t l, a;
   }lum;
   //texture fading out at the edges   
   for (size_t row = 0; row < sprite.height(); ++row) {
     float y = float(row) / (sprite.height() - 1) * 2 - 1;
     for (size_t col = 0; col < sprite.width(); ++col) {
       float x = float(col) / (sprite.width() - 1) * 2 - 1;
       // draw lightning, white in the center, and fades out toward the edges
       // at two rates
       if (abs(x) < 0.2)
         lum.l = mapRange(abs(x), 0.f, .2f, 255.f, 60.f);
       else
         lum.l = mapRange(abs(x), 0.2f, 1.f, 60.f, 0.f);
       lum.a = lum.l;
       sprite.write(&lum.l, col, row);
     }
   }
 }
Ejemplo n.º 5
0
        //--------------------------------------------------------------
        void DrawerBase::drawVectors(float x, float y, float renderWidth, float renderHeight)  const {
            if(enabled == false) return;
            
            ofPushStyle();
            if(useAdditiveBlending) ofEnableBlendMode(OF_BLENDMODE_ADD);
            else ofDisableAlphaBlending();

            int fw = _fluidSolver->getWidth();
            int fh = _fluidSolver->getHeight();

            //	int xStep = renderWidth / 10;		// every 10 pixels
            //	int yStep = renderHeight / 10;		// every 10 pixels
            
            ofPushMatrix();
            ofTranslate(x, y, 0);
            ofScale(renderWidth/(fw-2), renderHeight/(fh-2), 1.0);
            
            float maxVel = 5.0f/20000;
            
            Vec2f vel;
            float vt = velDrawThreshold * _fluidSolver->getInvWidth() * _fluidSolver->getInvHeight();
            vt *= vt;
            
            for (int j=0; j<fh-2; j+=vectorSkipCount+1 ){
                for (int i=0; i<fw-2; i+=vectorSkipCount+1 ){
                    vel = _fluidSolver->getVelocityAtCell(i+1, j+1);
                    float d2 = vel.lengthSquared();
                    if(d2>vt) {
                        if(d2 > maxVel * maxVel) {
                            float mult = maxVel * maxVel/ d2;
                            //				float mult = (d2 - maxVel * maxVel) / d2;
                            vel.x *= mult;
                            vel.y *= mult;
                        }
                        vel *= velDrawMult * 50000;
                        float b = mapRange(d2, vt, maxVel, 0.0f, brightness);
                        b = brightness*255;
//                        ofBeginShape();
//                        ofSetColor(0, 0, 0);
//                        ofVertex(i, j);
                        ofSetColor(b, b, b);
                        ofDrawLine(i, j, i + vel.x, j + vel.y);
//                        ofVertex(i + vel.x, j + vel.y);
//                        ofEndShape();
                    }
                }
            }
            ofPopMatrix();
            ofPopStyle();
        }
Ejemplo n.º 6
0
MemoryContext::Result MemoryContext::mapRegion(MemoryMap::Region region,
                                               Size size,
                                               Memory::Access access)
{
    Memory::Range range;
    Result r;

    if ((r = findFree(size, region, &range.virt)) != Success)
        return r;

    range.phys   = ZERO;
    range.size   = size;
    range.access = access;

    return mapRange(&range);
}
Ejemplo n.º 7
0
STDMETHODIMP ThwOverlayControlVMR9::get(int cap, int *val)
{
    if (!vmr9) {
        return E_UNEXPECTED;
    }
    if (!val) {
        return E_POINTER;
    }
    if (!isIn(cap, 1, 6) || !caps[cap]) {
        return E_INVALIDARG;
    }
    VMR9ProcAmpControl ctrl;
    ctrl.dwSize = sizeof(ctrl);
    if (vmr9->GetProcAmpControl(id, &ctrl) != S_OK) {
        return E_FAIL;
    }
    float v = ctrl.*(caps[cap]);
    *val = mapRange(v, std::make_pair(ranges[cap].MinValue, ranges[cap].MaxValue), std::make_pair(hwocMinMax[cap][0], hwocMinMax[cap][1]));
    return S_OK;
}
Ejemplo n.º 8
0
int main(void){
	//startLCD_Show_Credits();
    	//lcd_check_busy_flag();
    	//lcd_send_command( LCD_CLEAR );
    	//You can multiplex these pins by enabling and disabling the LCD display.
    	//lcd_puts( "Lx = 15uH " );
    	//lcd_check_busy_flag();
    	//lcd_send_command( LCD_SET_CURSOR_POS | LCD_SECOND_LINE );
    	//lcd_puts( "Cx = 150uF" );
	//lcd_check_busy_flag();
	//_delay_ms(2000);
	//lcd_send_command( LCD_CLEAR );
	//lcd_puts("Hello Jenn!");
	uart_init();
	stdout = &uart_tx;
	stdin = &uart_rx;
	init_ADC();

	//Set Pin Directions
	//Lights Pin as output
	LIGHTS_SIGNAL_DIR |= (1 << LIGHTS_SIGNAL);
	
	//Start Button Pin as input
	START_BUTTON_DIR &= ~(1 << START_BUTTON);
	
	//Stop Button Pin as Input
	STOP_BUTTON_DIR &= ~(1 << STOP_BUTTON);

	//Buzzer Pin as Output
	BUZZER_DIR |= (1 << BUZZER);

	//Trip Switch Pin as Input
	TRIP_SWITCH_DIR &= ~(1 << TRIP_SWITCH_PIN);

	//LCD Led Pin as output
	LCD_LED_DIR |= (1 << LCD_LED_PIN);
	LCD_LED_PORT |= (1 << LCD_LED_PIN); //Turn it on

	static long reading;
	static bool btnStop = false; 
	static bool tripSwitch = false;
	startLCD_Show_Credits();
	displayBlink(3);
	while (1){
		printf("Raw Reading: %d\t",read_ADC(0,&reading));
		long mapped_reading = mapRange(0,1023,0,1800,reading);
		printf("Mapped Reading: %d\t",mapped_reading);
		display_Selection(mapped_reading);
		if(START_BUTTON_PIN & (1 << START_BUTTON) && mapped_reading > 0){
			lcd_check_busy_flag();
			lcd_send_command(LCD_CLEAR);
			lcd_puts("    STARTING    ");
			displayBlink(2);
			long count = 0;
			_delay_ms(1000);
			//Turn on lights
			LIGHTS_SIGNAL_PORT |= (1 << LIGHTS_SIGNAL);
			bool lightsOn = true;
			for(count = mapped_reading;count >= 0;count--){
				//Check for stop button
				if(STOP_BUTTON_PIN & (1 << STOP_BUTTON)){
					lcd_check_busy_flag();
					lcd_send_command(LCD_CLEAR);
					lcd_puts("    STOPPING    ");
					btnStop = true;
					break;
				}
				//Pause if trip switch is high
				if(!(TRIP_SWITCH_PIN & (1 << TRIP_SWITCH))){
					if(!lightsOn){
						LIGHTS_SIGNAL_PORT |= (1 << LIGHTS_SIGNAL);
					}
					//If the the switch is still closed
					printf("UV Lights on for: %d more secs...\n",count);
					lcd_check_busy_flag();
					lcd_send_command(LCD_CLEAR);
					lcd_puts("    Exposing    ");
					lcd_check_busy_flag();
					lcd_send_command(LCD_SET_CURSOR_POS | LCD_SECOND_LINE);
				        char * zero = "0";	
					uint8_t mins = count / 60;
					uint8_t secs = count % 60;
					char minsStr[5];
					char secsStr[5];
					itoa(mins,minsStr,10);
					itoa(secs,secsStr,10);
					lcd_check_busy_flag();
					lcd_puts("      ");
					if(mins <= 9){
						lcd_check_busy_flag();
						lcd_puts(zero);
					}
					lcd_check_busy_flag();
					lcd_puts(minsStr);
					lcd_check_busy_flag();
					lcd_puts(":");
					if(secs <= 9){
						lcd_check_busy_flag();
						lcd_puts(zero);
					}
					lcd_check_busy_flag();
					lcd_puts(secsStr);  	
					_delay_ms(1000);
				}
				else{	
					lightsOn = false;
					LIGHTS_SIGNAL_PORT &= ~(1 << LIGHTS_SIGNAL);
					//Tell user the lid is open
					printf("LID OPEN, CLOSE IT TO CONTINUE\n");
					lcd_check_busy_flag();
					lcd_send_command(LCD_CLEAR);
					lcd_puts("    LID OPEN     ");
					lcd_check_busy_flag();
					lcd_send_command(LCD_SET_CURSOR_POS | LCD_SECOND_LINE);
					lcd_puts(" PLEASE CLOSE IT ");
					//Increment Count to make up for this one
					count++;
				}
				 	
			}
			//Turn off lights
			LIGHTS_SIGNAL_PORT &= ~(1 << LIGHTS_SIGNAL);
			if(btnStop != true){
				lcd_check_busy_flag();
				lcd_send_command(LCD_CLEAR);
				lcd_puts("    FINISHED    ");
				alarmSound(2);
			}
			else{
				buzzerSound(13);
				btnStop = false; //reset the stop flag
			}
			displayBlink(2);
		}	
	}
    	return 0;
}
Ejemplo n.º 9
0
void ntTruss::display_mode(D_mode m){
	///////////////////////////////////////////////////////////////
	///////////////////////////////////////////// UPDATE COLOR MODE
	this->m = m;
	int n = nodes.size();
	int e = elements.size();
	int rMax = 0;
	int gMax = 0;
	int bMax = 0;

	std::vector <float> qM;
	std::vector <float> fM;

	qMax = 0;
	fMax = 0;

	//UPDATE HOST COPY IF VALUES HAVE BEEN 
	if (isCurrent == false) {
		host_copy();
		isCurrent = true;
	}

	///////////////////////////////////////////////////////////////
	/////////////////////// DEFINE MAPPING BEHAVIOR OF DISPLAY MODE
	for (int i = 0; i < n; i++){
		float u = abs(Q_h.at(i * DOF + 0));
		float v = abs(Q_h.at(i * DOF + 1));
		float w = abs(Q_h.at(i * DOF + 2));

		float x = abs(F_h.at(i * DOF + 0));
		float y = abs(F_h.at(i * DOF + 1));
		float z = abs(F_h.at(i * DOF + 2));

		ntVec3 dQ = ntVec3(u, v, w);
		ntVec3 dF = ntVec3(x, y, z);
		float q = dQ.mag();
		float f = dF.mag();

		qM.push_back(q);
		fM.push_back(f);

		if (qMax < q){
			qMax = q;
			nMax = i + 1; ///REVISE TO INCLUDE NODES WITH EQUAL DISPLACEMENT
		}

		if (fMax < f){ fMax = f; }
		if (rMax < u){ rMax = u; }
		if (gMax < v){ gMax = v; }
		if (bMax < w){ bMax = w; }
	}
	///////////////////////////////////////////////////////////////
	////////////////////////////////////////////////// UPDATE COLOR
	if (m == vW) {
		for (int i = 0; i < n; i++){
			float r = .9;
			float g = .9;
			float b = .9;
			nodes.at(i)->col = Col4(r, g, b);
		}
		for (int i = 0; i < e; i++){
			elements.at(i)->w = .5;
		}
	}

	if (m == vQ) {
		for (int i = 0; i < n; i++){
			float q = qM.at(i);
			//float r = mapRange(0.3, 1, 0, qMax, q);
			//float g = mapRange(.35, .45, 0, qMax, q, false);
			//float b = mapRange(  0,.55, 0, qMax, q, false);
			float r = mapRange(0.3, 1, 0, qMax, q);
			float g = mapRange(0, .6, 0, qMax, q, false);
			float b = mapRange(0.1, .85, 0, qMax, q, false);
			nodes.at(i)->col = Col4(r, g, b);
		}
		for (int i = 0; i < e; i++){
			elements.at(i)->w = .5;
		}
	}
	if (m == vF) {
		for (int i = 0; i < n; i++){
			float q = fM.at(i);
			float r = mapRange( 0, 1, 0, qMax / 10000, q);
			float g = 0;//mapRange(0, .65, 0, qMax / 100000, q / 1000);
			float b = mapRange(0.75, .95, 0, qMax / 100000, q, false);
			if (r > .9){ g = 0;}
			if (b > .85){ g = 0; }
			nodes.at(i)->col = Col4(r, g, b);
		}
		for (int i = 0; i < e; i++){
			elements.at(i)->w = .5;
		}
	}
	if (m == vS) {
		for (int i = 0; i<n; i++){
			nodes.at(i)->col = ntCol4(1, 0, 0);
		}
		for (int i = 0; i < e; i++){
			float s = abs(S_h[i]);
			float r = mapRange(0.1, 1, 0, sMax, s);
			float g = mapRange(0.1, 1, 0, sMax, s);
			float b = mapRange(0.1, 1, 0, sMax, s);
			float a = mapRange(1, 1, 0, sMax, s);
			//float r = mapRange(0.3, 1, 0, sMax, s);
			//float g = mapRange(.35, .45, 0, sMax, s, false);
			//float b = mapRange(0, .55, 0, sMax, s, false);
			float w = mapRange(1, 1.5, 0, sMax, s);
			elements.at(i)->colE = Col4(r, g, b, a);
			elements.at(i)->colS = Col4(r, g, b, a);
			elements.at(i)->w = w;
		}
	}
}
Ejemplo n.º 10
0
void meshApp::init() {
	///////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////// OBJECTS
	int dimX, dimY;
	int dimU, dimV;
	dimX = dimY = 10;
	dimU = dimV = 50;
	mesh00 = ntPlane(dimX, dimX, dimU, dimV);

	///////////////////////////////////////////////////////////////
	/////////////////////////////////////// OBJECTS COLOR BY VERTEX

	for (int i = 0; i < mesh00.verts.size(); i++){
		int val = mesh00.faces.size();
		float r = mapRange(.5,  1, 0, val, i, true);
		float g = mapRange(.25, .4, 0, val, i,false);
		float b = mapRange( 0, .3, 0, val, i, true);
		float a = mapRange(-.25, 1, 0, val, i, true);
		float s = mapRange(1, 5, 0, val, i, true);
		mesh00.verts.at(i)->setSize(s);
		mesh00.verts.at(i)->set_color(ntColor4f(r, g, b, 1));
	}
	
	for (int i = 0; i < mesh00.faces.size(); i++){
		int val = mesh00.faces.size();
		float n = mapRange(.01,.25, 0, val, i, false);
		float w = mapRange(.01, 3, 0, val, i, false);
		float r = mapRange(.25, .5, 0, val, i, false);
		float g = mapRange(0, .25, 0, val, i, true);
		float b = mapRange(0, .8, 0, val, i, true);
		float a = mapRange(.5, 1, 0, val, i, false);
		mesh00.faces.at(i).normal.setLength(n);
		mesh00.faces.at(i).normal.colS = ntColor4f(.55, .65, .50, a);
	}

	mesh00.addPhysics(true);
	mesh00.fixCorners();

	std::vector<Particle*>* particlesPtr00 = mesh00.getParticles();
	std::vector<Spring*>* springsPtr00 = mesh00.getSprings();

	force00 = ntBasePhysics(ntVec3(0, 0, 0), .01);
	force00.setGravity(-.0001);
	//force00.setForce(Vec3(5, 0, 0), .1);
	std::cout << springsPtr00->size() << endl;

	force00.setParticles(particlesPtr00);
	force00.setSprings(springsPtr00);

	int ps = particlesPtr00->size();

	for (int i = 0; i < 3; i++){
		int pFixed = 465+i;// (rand() % ps);
		//particlesPtr00->at(pFixed)->fixed = true;
	}
}
Ejemplo n.º 11
0
  // generate a bolt of lightning and add to our mesh
  // modified from Michael Hoffman's
  // (http://gamedevelopment.tutsplus.com/tutorials/how-to-generate-shockingly-good-2d-lightning-effects--gamedev-2681)
  void makeBolt(Vec3f source, Vec3f dest, int maxBranches = 0,
            float branchProb = 0.01, float wid = 0.05, int n = 80) {

    Vec3f tangent = (dest - source);  // direction of lightning
    Vec3f normal =
        tangent.cross(Vec3f(0, 0, -1)).normalize();  // normal to lightning
    float length = tangent.mag();

    // choose n random positions (0,1) along lightning vector and sort them
    vector<float> positions;
    positions.push_back(0);
    for (int i = 0; i < n; i++) positions.push_back(rnd::uniform());
    sort(positions.begin(), positions.end());

    float sway = 1;  // max random walk step of displacement along normal
    float jaggedness = 1 / sway;

    Vec3f prevPoint = source;
    float prevDisplacement = 0;
   
    //float width = 0.06; 
    float width = (wid > 0) ? wid : length / 25.0 + 0.01;
    //color = Color(1, 0.7, 1, 1);
    int branches = 0;

    mesh.primitive(Graphics::TRIANGLES);

    Vec3f point;

    for (int i = 1; i < n; i++) {
      float pos = positions[i];

      // used to prevent sharp angles by ensuring very close positions also have
      // small perpendicular variation.
      float scale = (length * jaggedness) * (pos - positions[i - 1]);

      // defines an envelope. Points near the middle of the bolt can be further
      // from the central line.
      float envelope = pos > 0.95f ? mapRange(pos, 0.95f, 1.0f, 1.0f, 0.0f) : 1;

      // displacement from prevDisplacement (random walk (brownian motion))
      float displacement = rnd::uniformS(sway) * scale + prevDisplacement;
      displacement *= envelope;

      // calculate point, source plus percentage along tangent, and displacement
      // along normal;
      point = source + pos * tangent + displacement * normal;

      // generate triangles (vertices and texture coordinates) from point and
      // prevPoint
//**********
      mesh.vertex(prevPoint + normal * width);
      mesh.vertex(prevPoint - normal * width);
      mesh.vertex(point + normal * width);
      mesh.vertex(point + normal * width);
      mesh.vertex(prevPoint - normal * width);
      mesh.vertex(point - normal * width);
      mesh.texCoord(0, (float)(i - 1) / n);
      mesh.texCoord(1, (float)(i - 1) / n);
      mesh.texCoord(0, (float)(i) / n);
      mesh.texCoord(0, (float)(i) / n);
      mesh.texCoord(1, (float)(i - 1) / n);
      mesh.texCoord(1, (float)(i) / n);
      mesh.color(color);
      mesh.color(color);
      mesh.color(color);
      mesh.color(color);
      mesh.color(color);
      mesh.color(color);
//**********
      if (branches < maxBranches && rnd::prob(branchProb)) {
        branches++;
        Vec3f dir(tangent);
        rotate(dir, Vec3f(0, 0, 1), rnd::uniformS(30) * M_DEG2RAD);
        dir.normalize();
        float len = (dest - point).mag();
        makeBolt(point, point + dir * len * 0.4, maxBranches - 1, branchProb, width * 0.6,
             n * 0.5);
      }
      // remember previous point and displacement for next iteration
      prevPoint = point;
      prevDisplacement = displacement;
    }
    //generate last piece
    mesh.vertex(point + normal * width);
    mesh.vertex(point - normal * width);
    mesh.vertex(dest + normal * width);
    mesh.vertex(dest + normal * width);
    mesh.vertex(point - normal * width);
    mesh.vertex(dest - normal * width);
    mesh.texCoord(0, (float)(n - 1) / n);
    mesh.texCoord(1, (float)(n - 1) / n);
    mesh.texCoord(0, (float)(n) / n); //0
    mesh.texCoord(0, (float)(n) / n); //0
    mesh.texCoord(1, (float)(n - 1) / n);
    mesh.texCoord(1, (float)(n) / n); //1
    mesh.color(color);
    mesh.color(color);
    mesh.color(color);
    mesh.color(color);
    mesh.color(color);
    mesh.color(color);
  }
Ejemplo n.º 12
0
int main (void){
	//Start UART
	init_UART();
	stdout = &uart_tx;
	stdin = &uart_rx;
	
	//Start ADC
	init_ADC();

	//Set pins for display as output
	//Digit 1
	DDRD |= (1 << PD5); //A
	DDRD |= (1 << PD6); //B
	DDRD |= (1 << PD7); //C
	DDRB |= (1 << PB0); //D
	
	//Digit 2
	DDRC |= (1 << PC1); //A
	DDRC |= (1 << PC2); //B
	DDRC |= (1 << PC3); //C
	DDRC |= (1 << PC4); //D

	//Lights Pin Output
	DDRC |= (1 << PC5); 
 
	//Start/Stop Button
	DDRB &= ~(1 << PB1); //Start button input	
	DDRB &= ~(1 << PB2); //Stop button input

	//Buzzer Pin
	DDRD |= (1 << PD2); //Output

   	int first_digit = 9;
	int second_digit = 10;    
    	static long reading;
	static bool btnStart;

	//Blink display to let me know everything is good!
	displayBlink(5);
	while(1) {
		printf("Raw Reading: %d\t",read_ADC(0,&reading));
		long mapped_reading = mapRange(0,1023,0,99,reading);
		printf("Mapped Reading: %d\n",mapped_reading);
		display_Selection(mapped_reading);
		if(PINB & (1 << PB1) && mapped_reading > 0 ){ //If the Start button gets pressed then
			displayBlink(2);
			int count = 0;
			_delay_ms(1000);
			PORTC |= (1 << PC5);
			for(count = mapped_reading;count >= 0;count--){
				//Count Backwards from that number	
				//Check for stop button quit if set
				if(PINB & (1 << PB2))
					break;

				uint8_t fdigitm = count / 10;
				uint8_t sdigitm = count % 10;
				firstDisplay(fdigitm);
				secondDisplay(sdigitm);
				printf("UV Lights on for: %d more secs...\n",count);
				_delay_ms(1000);
			}	
			PORTC &= ~(1 << PC5);
			//displayBlink(5);
			alarmSound(2);
			displayBlink(2);
		}
	}
}