Can the if--else if -- else inside while work?

#include <SPI.h>

// set pin 53 as the slave select for the digital pot:
const int slaveSelectPin = 53; //WAS 10 on Arduino uno.
const int shutdownPin = 7; // was 7 on uno.
const int wiper0writeAddr = B00000000;
const int wiper1writeAddr = B00010000;
const int tconwriteAddr = B01000000;

const int tcon_0off_1on = B11110000;
const int tcon_0on_1off = B00001111;
const int tcon_0off_1off = B00000000;
const int tcon_0on_1on = B11111111;

const int analogInPin_c = A5;  // center Vc=Vss/2 from the PAD, use for set ini V. here no use, Vc directly goes to pad.

const int analogInPin_x = A1;  // Analog input pin that the potentiometer is attached to
const int analogInPin_y = A2;  // Analog input pin that the potentiometer is attached to

const int analogOutPin_x = wiper0writeAddr; // write MCP POT
const int analogOutPin_y = wiper1writeAddr; // write MCP POT

int sensorValue_c = 0;        // value read from the PAD, Vc=Vss/2
int sensorValue_x = 0;        // value read from the PAD, Vc=Vss/2
int sensorValue_y = 0;        // value read from the PAD, Vc=Vss/2

//?int outputValue_c = 0;        // value output to the PWM (analog out)
int outputValue_x = 0;        // mid variable stored the value from 'sensorValue_c' and MAP that for use
int outputValue_y = 0;        // mid variable stored the value from 'sensorValue_c' and MAP that for use

int Vcr = 0;        // value read from the PAD, Vc=Vss/2
int Vc = 0;        // value read from the PAD, Vc=Vss/2
const int LEDpin1 = 47;
const int LEDpin2 = 46;
int count = 0;

void setup() {
  Serial.begin(9600);

  // set the slaveSelectPin as an output:
  pinMode (slaveSelectPin, OUTPUT);
  // set the shutdownPin as an output:
  pinMode (shutdownPin, OUTPUT);
  // start with all the pots shutdown
  digitalWrite(shutdownPin, LOW);
  // initialize SPI:
  SPI.begin();
  Serial.println("Setup complete");
  //??
  Serial.println("Starting loop");
  digitalWrite(shutdownPin, HIGH); //Turn off shutdown
}

void loop() {
  AnalogInOut(); //
  ArduinoDAC(); //
}
void AnalogInOut() {
  // take from some where used for testing joystick control LED,
  //need change to get date fron RC and write to MCP pots
  // read the analog in value:

  Vcr = analogRead(analogInPin_c);
  delay(1000);
  Serial.print("Vcr= ");
  Serial.println(Vcr);
  Vc = map(Vcr, 0, 1023, 165, 225);
  Serial.print("Vc = ");
  Serial.println(Vc);
  delay(10);
  Serial.print("count_ini= ");
  Serial.println(count);

  /////if (count = 0)
  while (count = 0)
  {
    if (Vc > 188)
    {
      digitalWrite(LEDpin1, HIGH);
      Serial.print("Vc TOO HIGH: ");
      Serial.println(Vc);
      exit(0);
      delay(10);
    }
    else if (Vc < 165) {
      digitalWrite(LEDpin2, HIGH);
      Serial.print("Vc TOO LOW = : ");
      Serial.println(Vc);
      exit(0);
      delay(10);
    }
    else {

      Serial.print("analogInPin_c_T00: ");
      Serial.println(analogInPin_c);

      /// digitalPotWrite(wiper0writeAddr, Vc);
      ///digitalPotWrite(wiper1writeAddr, Vc);

      digitalPotWrite(wiper0writeAddr, 100);
      digitalPotWrite(wiper1writeAddr, 220);

      count = 1;
      Serial.print("count_new= ");
      Serial.println(count);
    }
    delay(15000);
  }

  ////////////////////////////////////////////////////////////////////////////////////////////////////////////

  sensorValue_x = analogRead(analogInPin_x);
  sensorValue_y = analogRead(analogInPin_y);
  // map it to the range of the analog out:
  outputValue_x = map(sensorValue_x, 0, 1023, 70, 248);
  outputValue_y = map(sensorValue_y, 0, 1023, 70, 248);  // change the analog received to the value suite for PAD joystick


  //   print the results to the Serial Monitor:
  Serial.println("sensor_x =_T10 ");
  Serial.println(sensorValue_x);
  Serial.println("sensor_y = ");
  Serial.println(sensorValue_y);
  Serial.println("\t output = ");
  Serial.println(outputValue_x);
  Serial.println(outputValue_y);

  // wait 2 milliseconds before the next loop for the analog-to-digital
  // converter to settle after the last reading:
  delay(2);
}


void ArduinoDAC() {
  Serial.println("Starting loop");
  digitalWrite(shutdownPin, HIGH); //Turn off shutdown

  digitalPotWrite(wiper0writeAddr, outputValue_x); // Set wiper 0 to 200

  delay(5);

  digitalPotWrite(wiper1writeAddr, outputValue_y); // Set wiper 1 to 200

  delay(5);
}

// This function takes care of sending SPI data to the pot.
void digitalPotWrite(int address, int value) {
  /// Serial.print("wiper0 position_t30: ");////
  /// Serial.println();////
  // take the SS pin low to select the chip:
  digitalWrite(slaveSelectPin, LOW);
  //  send in the address and value via SPI:
  SPI.transfer(address);
  SPI.transfer(value);
  delay(30);
  // take the SS pin high to de-select the chip:
}

int digitalPotRead(int address) {
  // take the SS pin low to select the chip:
  digitalWrite(slaveSelectPin, LOW);
  //  send in the address via SPI:
  SPI.transfer(address);
  // send zero to read data from the address
  delay(30);
}

Yes

GrOnThOs:
Yes

Thank you GrOnThOs,

The code is too mess to be attached.
shortly as:

while ()
{
if (){

}

else if () {

}

else () {

}
delay(5000);
}

The [delay(5000);] doesn't work, I mean the sketch didn't execute this line.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Post the full code please?
What do you want your code to do?
Why are you using while()?

Thanks.. Tom.. :slight_smile:

else {
 
}

not

else () {

}
.
.
.  while (count = 0)
  {
    if (Vc > 188)
    {
      digitalWrite(LEDpin1, HIGH);
      Serial.print("Vc TOO HIGH: ");
      Serial.println(Vc);
      exit(0);
      delay(10);
    }
.
.
.
  1. "count = 0" should be "count == 0".

  2. Why do you use exit(0) here?

GrOnThOs:

else {

}




not



else () {

}

Thank you GrOnThOs, actually there wasn't a '()' in my original code as attached. Sorry the mistake by short.
still the [delay 5000 ] didn't executed no matter put where:

else {
delay(5000);
}
// or
delay(5000);
}

Blackfin:

.

.
.  while (count = 0)
  {
    if (Vc > 188)
    {
      digitalWrite(LEDpin1, HIGH);
      Serial.print("Vc TOO HIGH: ");
      Serial.println(Vc);
      exit(0);
      delay(10);
    }
.
.
.




1) "count = 0" should be "count == 0".

2) Why do you use exit(0) here?

Thank you Blackfin.

  1. the purpose to use "count == 0" is let the code just do ONE time this if section, no need loop here, there should be some other solution.
    I put "count == 0", there is error came: expected initializer before '==' token.

  2. exit(0) because if the date value is not match, I need to do some repair.

if(count == 0)?

Hi,
What is your code supposed to do?
What is your code doing?
What is the application?
What model Arduino are you using?

Thanks.. Tom.. :slight_smile:

Try "while (count >= 0)" and change "count = 1;" to "count ++;"

Thank you for reply, I just post the code, questions are:

  1. the 'while - if' section doesn't run, I put a 'delay 5000', never wait;
  2. the 'count' work as a mark, let the 'while - if' section running just once, is this OK?
  3. If I put the 'while - if' section inside the void setup, any solution to 'the digitalPotWrite didn't declared in this scope' ?