xxx1590:
Hi, I am having trouble using switch case to pick up the value from my array...can anyone please help spot the problem? Thank you very much..
It looks like your intent is more like:
// These constants won't change. They're used to give names to the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
//const int threshold = 400; // an arbitrary threshold level that's in the range of the analog input
const int sensorMin = 0; // sensor minimum, discovered through experiment
const int sensorMax = 1023;
const unsigned char screen_val[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
const int range[] = {
0,1,2, 3, 4, 5, 6, 7, 8, 9, }; // Pin numbers for some kinf of output?
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
Serial.begin(9600);
}
void deal(unsigned char value) {
for (int i=0; i<8; i++)
{
digitalWrite(range[i], !bitRead(value, i));
}
}
void loop() {
// read the sensor:
int sensorReading = analogRead(A0);
// map the sensor range to a range of four options:
int i = map(sensorReading, sensorMin, sensorMax, 0, 5);
// do something different depending on the range value:
switch (i) {
case 1: // your hand is close to the sensor
Serial.println("1");
deal(screen_val[1]);
break;
case 2: // your hand is a few inches from the sensor
Serial.println("2");
break;
case 3: // your hand is nowhere near the sensor
Serial.println("3");
break;
case 4:
Serial.println("4");
break;
case 5:
Serial.println("5");
break;
}
delay(1000);
}
WARNING: You appear to be using Pin 1 for digital output at the same time you are using Serial. This won't work on an Arduino UNO since Pin 0 and Pin 1 are used for Serial.