task main() {
    int _chVal = 0;

    nxtDisplayCenteredTextLine(0, "HiTechnic");
    nxtDisplayCenteredBigTextLine(1, "Proto");
    nxtDisplayCenteredTextLine(3, "Test 1");
    nxtDisplayCenteredTextLine(5, "Connect HTPB");
    nxtDisplayCenteredTextLine(6, "to S1");

    wait1Msec(2000);
    // Setup all the digital IO ports as outputs (0xFF)
    if (!HTPBsetupIO(HTPB, 0xFF)) {
        nxtDisplayTextLine(4, "ERROR!!");
        wait1Msec(2000);
        StopAllTasks();
    }

    while(true) {
        eraseDisplay();
        // get the value for ADC channel 0, we want a 10 bit answer
        _chVal = HTPBreadADC(HTPB, 0, 10);
        nxtDisplayTextLine(4, "A0: %d", _chVal);

        // if _chVal is more than 512, turn on the LED, otherwise turn it off.
        if (_chVal > 512) {
            if (!HTPBwriteIO(HTPB, 0xFF)) nxtDisplayTextLine(5, "ERR WRITE");
        } else {
            if (!HTPBwriteIO(HTPB, 0x00)) nxtDisplayTextLine(5, "ERR WRITE");
        }
        wait1Msec(100);
    }
}
Ejemplo n.º 2
0
void writeToProtoboard(int data)
{
    int err;

    err = HTPBwriteIO(HTPB, data);
    if (!err) {
        nxtDisplayBigTextLine(4, "Error writing to protoboard, %d", err);
    }

    wait1Msec(50);

    /*
     * Toggle the interrupt Pin
     */
    err = HTPBwriteIO(HTPB, (0b0111 & data));
    if (!err) {
        nxtDisplayBigTextLine(4, "Error toggling interrupt pin, %d", err);
    }

    wait1Msec(50);

    err = HTPBwriteIO(HTPB, (0b1111 & data));
    if (!err) {
        nxtDisplayBigTextLine(4, "Error toggling interrupt pin, %d", err);
    }

}
task main() {
  // The data to be written: 0x3F = 111111 binary,
  // makes all digital ports outputs.
  HTPBsetupIO(HTPB, 0x3F);

  while(true) {
    // Switch off LED on port B0
    HTPBwriteIO(HTPB, 0x00);
    sleep(30);
    wolight = HTPBreadADC(HTPB, 0, 10);

    // Switch on LED on port B0
    HTPBwriteIO(HTPB, 0x01);
    sleep(30);
    wlight = HTPBreadADC(HTPB, 0, 10);

    // Calculate the difference
    lightdelta = wlight - wolight;

    eraseDisplay();
    displayTextLine(1, "%4d", wlight);
    displayTextLine(2, "%4d", wolight);
    displayTextLine(3, "%4d", lightdelta);
    sleep(30);
  }
}
Ejemplo n.º 4
0
task main() {
  // The data to be written: 0x30 = 110000 binary,
  // makes B4,B5 digital ports outputs.
  HTPBsetupIO(HTPB, 0x30);

  while(true) {

    // Turn off the LED
    HTPBwriteIO(HTPB, 0x00);

    eraseDisplay();
    nxtDisplayTextLine(2, "running");

    // Wait a random time between 5 and 10 seconds.
    wait1Msec(random(5000) + 5000);

    // Switch on the LED and reset the timer
    HTPBwriteIO(HTPB, 0x10);
    time1[T1] = 0;

    // Wait for user to press the stop button
    while (HTPBreadIO(HTPB, 0xF3) != 0x01) {
      wait1Msec(5);
    }

    eraseDisplay();
    nxtDisplayTextLine(2, "%d", time1[T1]);

    // Wait for user to reset
    while (HTPBreadIO(HTPB, 0xF3) != 0x03) {
      wait1Msec(5);
    }
  }
}
Ejemplo n.º 5
0
task readi2c() {
  while(true) {
    if(HTPBreadIO(HTPB, 0xF3) == 0) {
      eraseDisplay();
      nxtDisplayTextLine(1, "Magnet present");
      HTPBwriteIO(HTPB, 0x10);
    } else {
      eraseDisplay();
      nxtDisplayTextLine(1, "Magnet absent");
      HTPBwriteIO(HTPB, 0x00);
    }
    wait1Msec(50);
  }
}
task main() {
  // Local variables
  int inputdata;
  ubyte outputdata;

  // The data to be written: 0x3F = 111111 binary,
  // makes all digital ports outputs.
  HTPBsetupIO(HTPB, 0x3F);

  while(true) {
    // Read a 10bit wide analogue value from A0
    inputdata = HTPBreadADC(HTPB, 0, 10);

    eraseDisplay();
    nxtDisplayTextLine(1, "%d", inputdata);

    // Depending on the input voltage on A0,
    // turn on the corresponding LED.
    outputdata = 0x01;
    if (inputdata >  31) outputdata = 0x02;
    if (inputdata >  63) outputdata = 0x04;
    if (inputdata > 127) outputdata = 0x08;
    if (inputdata > 255) outputdata = 0x10;
    if (inputdata > 511) outputdata = 0x20;

    HTPBwriteIO(HTPB, outputdata);
    wait1Msec(50);
  }
}
Ejemplo n.º 7
0
task main() {
  // Local variables
  int soundlevel;
  ubyte outputdata;

  // The data to be written: 0x3F = 111111 binary,
  // makes all digital ports outputs.
  HTPBsetupIO(HTPB, 0x3F);

  while(true) {
    // Get the value from the LEGO sound sensor.
    soundlevel = 1023 - SensorValue[SOUND_PORT];

    eraseDisplay();
    nxtDisplayTextLine(1, "%d", soundlevel);

    // Depending on the input voltage on A0,
    // turn on the corresponding LED.
    outputdata = 0x01;
    outputdata = 0x01;
    if (soundlevel >  65) outputdata = 0x02;
    if (soundlevel > 108) outputdata = 0x04;
    if (soundlevel > 180) outputdata = 0x08;
    if (soundlevel > 300) outputdata = 0x10;
    if (soundlevel > 500) outputdata = 0x20;

    HTPBwriteIO(HTPB, outputdata);
    wait1Msec(50);
  }
}
Ejemplo n.º 8
0
task main() {
  // The data to be written: 0x10 = 010000 binary,
  // makes B4 digital port an output.
  HTPBsetupIO(HTPB, 0x10);

  while(true) {
    if(HTPBreadIO(HTPB, 0xF3) == 0) {
      eraseDisplay();
      nxtDisplayTextLine(1, "Magnet present");
      HTPBwriteIO(HTPB, 0x10);
    } else {
      eraseDisplay();
      nxtDisplayTextLine(1, "Magnet absent");
      HTPBwriteIO(HTPB, 0x00);
    }
    wait1Msec(50);
  }
}
Ejemplo n.º 9
0
task main() {
  // The data to be written: 0x3F = 111111 binary,
  // makes B4 digital port an output.
  HTPBsetupIO(HTPB, 0x3F);

  while(true) {
    // Read the value from the temp sensor
    inputdata = HTPBreadADC(HTPB, 0, 10);

    // Convert to an actual temperature
    temperature = ((inputdata - 186) * 32 / 99);
    eraseDisplay();
    nxtDisplayTextLine(1, "%d", temperature);

    // If we're above 28 degrees, switch on the LED
    if(temperature > THRESHOLD) {
      HTPBwriteIO(HTPB, 0x10);
    } else {
      HTPBwriteIO(HTPB, 0x00);
    }
    wait1Msec(50);
  }
}
task main() {
  // The data to be written: 0x30 = 110000 binary,
  // makes B4,B5 digital ports outputs.
  HTPBsetupIO(HTPB, 0x30);

  while(true) {
    // Turn off all outputs
    HTPBwriteIO(HTPB, 0x00);

    eraseDisplay();
    nxtDisplayTextLine(2, "running");

    wait1Msec(random(5000) + 5000);

    // Turn on a random LED
    if(random(1) > 0) {
      HTPBwriteIO(HTPB, 0x10);
      buttonmask = 0x01;
    } else {
      HTPBwriteIO(HTPB, 0x20);
      buttonmask = 0x02;
    }

    time1[T1] = 0;

    while ((ubyte)HTPBreadIO(HTPB, 0xF3) != buttonmask) {
      wait1Msec(5);
    }

    eraseDisplay();
    nxtDisplayTextLine(2, "%d", time1[T1]);

    while (HTPBreadIO(HTPB, 0xF3) != 0x03) {
      wait1Msec(5);
    }
  }
}
task main() {
  // Local variables
  int inputdata;

  // The data to be written: 0x3F = 111111 binary,
  // makes all digital ports outputs.
  HTPBsetupIO(HTPB, 0x3F);

  while(true) {
    // Read a 10bit wide analogue value from A0
    inputdata = HTPBreadADC(HTPB, 0, 10);
    eraseDisplay();
    nxtDisplayTextLine(1, "%d", inputdata);

    // If A0 is less than 50% of the max value
    // turn off the LED, otherwise switch it on
    if(inputdata < 512)
      HTPBwriteIO(HTPB, 0x00);
    else
      HTPBwriteIO(HTPB, 0x01);

    wait1Msec(50);
  }
}
task main() {
  int _chVal = 0;  // analog input
  byte inputs = 0;  // all digital inputs
  int value = 0;
  int touchValue = 0;
  int touchValue2 = 0;

  nxtDisplayCenteredTextLine(0, "HiTechnic");
  nxtDisplayCenteredBigTextLine(1, "Proto");
  nxtDisplayCenteredTextLine(3, "Test 1");
  nxtDisplayCenteredTextLine(5, "Connect HTPB");
  nxtDisplayCenteredTextLine(6, "to S1");

  wait1Msec(2000);
  // Setup all the digital IO ports as outputs (0x30) 110000 pins B4/B5 = outputs, others are inputs.
  if (!HTPBsetupIO(HTPB, 0x30)) {
    nxtDisplayTextLine(4, "ERROR!!");
    wait1Msec(2000);
    StopAllTasks();
  }

  while(true) {
    value = 0;
  	eraseDisplay();
    // get the value for ADC channel 0, we want a 10 bit answer
		int j = 0;
		for(int i=0; i<5; i++)
		{

				_chVal = HTPBreadADC(HTPB, j, 10);
				switch(j) {
					case 0:  nxtDisplayTextLine(0, "A0: %d", _chVal);
					if(_chVal>512) value+=(1<<8);
					break;
					case 1:  nxtDisplayTextLine(1, "A1: %d", _chVal);
					if(_chVal>512) value+=(1<<7);
					break;
					case 2:  nxtDisplayTextLine(2, "A2: %d", _chVal);
					if(_chVal>512) value+=(1<<6);
					break;
					case 3:  nxtDisplayTextLine(3, "A3: %d", _chVal);
					if(_chVal>512) value+=(1<<5);
					break;
					case 4:  nxtDisplayTextLine(4, "A4: %d", _chVal);
					if(_chVal>512) value+=(1<<4);
					break; }

    	  j++;

  }
  inputs = HTPBreadIO(HTPB, 0x3F);
  nxtDisplayTextLine(5, "D: 0x%x", inputs);
  value+=(inputs&0x01)<<3;
  value+=(inputs&0x02)<<1;
  value+=(inputs&0x04)>>1;
  value+=(inputs&0x08)>>3;
  nxtDisplayBigTextLine(6, "%d", mapNineBitToDegrees(value));
  touchValue = SensorValue[S2];
  touchValue2 = SensorValue[S3];
  if(touchValue == 1 && touchValue2 == 1) { HTPBwriteIO(HTPB, 0x30); }  // turn on B4 and B5
  else if(touchValue == 1) { HTPBwriteIO(HTPB, 0x10); }  // turn on B4
  else if(touchValue2 == 1)  { HTPBwriteIO(HTPB, 0x20); }  // turn on B5
  else { HTPBwriteIO(HTPB, 0x00); } // turn both B4 and B5 off
  wait1Msec(50);
}
}