switch statements(SOLVED)

switch statements: I have successfully completed two programs that tested good. problem is I want to combine the two. The first counts button presses,(0-3) and assigns a case statement to each number, and the second reads four directional switches and turns on one of two motors either forwards or backwards depending on the switch is active. Inputs are pulled high. Included are excerpts of the two programs. what I want to do is replace the analogWrite(motor___Pin,150) with the analogwrite(motorpin,___) statement from the case statements.

<>const int [4,5,6,7,]
const.int. modepin=2
const.int. motor pin=9

void loop()
val = digital read(mode pin);
if(val != butstate &&val==LOW) {
mode++;
} butstate==val;
if(modestate != mode {
//if(mode != 0) {
switch (mode) {
case 2:

digitalWrite(ledpinone, LOW);
digitalWrite(ledpintwo, HIGH);
digitalWrite(ledpinthree,LOW);
digitalWrite(ledpinfour,LOW);
analogWrite(motorPin, 150);
break;

excerpt #2

void loop() {

buttonOneState = digitalRead(buttonOnePin);
buttonTwoState = digitalRead(buttonTwoPin); //three more states after these two.

//compare the buttonstate to the previous state.
if(buttonOneState != lastbuttonOneState)
if(buttonOneState==LOW) {
analogWrite(motorOnePin,___);
digitalWrite(dirOnePin, HIGH); //dirpins are for directional control on a L293B chip

} else {
analogWrite(motorOnePin,0);
digitalWrite(dirOnePinLOW);

Neither of those snippets are valid code and they're useless with the rest of their programs and some explanation of what you're actually trying to achieve.

You say you want to combine two programs you have that both work. So have a go and post your best attempt at doing that (in code tags </> please). Then say what it does and what you want it to do that's different.

Steve

I am sending complete codes of my project. I someone could please check and donate much needed info, it would be highly appreciated.

code/code
const int westPin = 2; //digital input directions
const int northPin = 6; //
const int eastPin = 4; //
const int southPin = 5; //
const int modePin = 3; // edge detect
const int ledPinOne=7; // digital output
const int ledPinTwo=8; //
const int ledPinThree=14; //
const int ledPinFour = 15; //
const int rmotorPin = 9; // analog output
const int dmotorPin = 11; //
const int rdirPin = 10; //digital output
const int ddirPin = 12; //

int westPinState =0 ; // all variables
int lastWestPinState =0; //
int northPinState =0; //
int lastNorthPinState = 0; //
int eastPinState = 0; //
int lastEastPinState = 0; //
int southPinState = 0; //
int lastSouthPinState = 0; //
int mode = 0; //
int val = 0; //
int buttonState = 0; //
int modeState = 0; // all variables

void setup () {
pinMode(westPin,INPUT); // setup input and outputs and turn on internal pullups
pinMode(northPin,INPUT); //
pinMode(eastPin,INPUT);
pinMode(southPin,INPUT);
pinMode(modePin);
pinMode(2,INPUT_PULLUP);
pinMode(3,INPUT_PULLUP):
pinMode(4,INPUT_PULLUP);
pinMode(5,INPUT_PULLUP);
pinMode(6,INPUT_PULLUP);
pinMode(ledPinOne,OUTPUT);
pinMode(ledPinTwo,OUTPUT);
pinMode(ledPinThree,OUTPUT);
pinMode(ledPinFour,OUTPUT);
pinMode(rmotor,OUTPUT); //next four pins pertain to the L298 motor driver
pinMode(dmotor,OUTPUT);
pinMode(rdirPin,OUTPUT);
pinMode(ddirPin,OUTPUT);

}
void loop()

val = digitalRead(modePin); {
if(val != buttonState && val==LOW) {
mode++;
}
buttonState = val;
if(modeState != mode) {
//if(mode != 0) {
}
switch (mode) {

case 2:
digitalWrite(ledPinOne,LOW);
digitalWrite(ledPinTwo,HIGH);
digitalWrite(ledPinthree,LOW);
digitalWrite(ledPinFour,LOW);
analogWrite(rmotorPin,150);
analogWrite(dmotorPin,100);
break;
case 3:
digitalWrite(ledPinOne,LOW);
digitalWrite(ledPinTwo,LOW);
digitalWrite(ledPinThree,HIGH);
digitalWrite(ledPinFour,LOW);
analogWrite(rmotorPin,200);
analogWrite(dmotorPin,125);
break;
case 4:
digitalWrite(ledPinOne,LOW);
digitalWrite(ledPinTwo,LOW);
digitalWrite(ledPinThree,LOW);
digitalWrite(ledPinFour,HIGH);
analogWrite(rmotor,250);
analogWrite(dmotor,150);
break;
default:
mode = 1;
digitalWrite(ledPinOne,HIGH);
digitalWrite(ledPinTwo,LOW);
digitalWrite(ledPinThree,LOW);
digitalWrite(ledPinFour,LOW);
analogWrite(rmotor,100);
analogWrite(dmotor,65);
break;
}
westPinState = digitalRead(westPin);
northPinState = digitalRead(northPin);
eastPinState = digitalRead(eastPin);
southPinState = digitalRead(southPin);

// compare the buttonstate with the previous state
if(westPinState != lastWestPinState) {
if( westPinState==LOW) {
analogWrite(rmotorPin,mode);
digitalWrite(rdirPin,LOW);
} else
{
analogWrite(rmotorPin,0);
digitalWrite(rdirPin,LOW);
}
}
if (northPinStdate != lastNorthPinState) {
if(northPinState==LOW) {

analogWrite(dmotorPin,mode);
digitalWrite(ddirPin,LOW);
}
else {
analogWrite(dmotorPin,0);
digitalWrite(ddirPij,LOW);
}
}
if (eastPinState != lastEastPinState) {
if(eastPinState ==LOW) {

analogWrite(rmotorPin,mode);
digitalWrite(rdirPin,High);
}
else {
analogWrite(rmotorPin,0);
digitalWrite(rdirPin,LOW);
}
}
if(southPinState != lastSouthPinState) {
if(southPinState==LOW) {

analogWrite(dmotorPin,mode);
digitalWrite(ddirPin,HIGH);
}
else {
analogWrite(dmotorPin,0);
digitalWrite(ddirPin,LOW);
}
}
delay(50)
lastWestPinState = westPinState;
lastNorthPinState = northPinState;
lastEastPinState = eastPinState;
lastSouthPinState = southPinState;

}

The first part of the code generates my edge detection with 4 cases. the second reads the directional switches and sends info to mount motors on my telescope. I want to put the analog write values on to the motors but the motors don't seam to respond to the n,e,w,s, switches. the declination runs in response to the for speeds but never turns off.

Use CTRL T to format your code.

Attach your sketch between code tags
[code]Paste your sketch here[/code]

pinMode(modePin); // ??? ?

analogWrite(dmotorPin,mode); // what is the range you expect for ‘mode’ ?

not sure what you mean by range, the sw. statements set the range at preset levels and could change at my discretion. help to figure this out.

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.

Thanks.. Tom... :slight_smile:

I tried that but it didn't work like I expected, and I don't know how to paste code, I am almost seventy years old and i'm sort of forced into this.

  pinMode(3, INPUT_PULLUP):

Whoops

Hi,
Does your code compile?

You don't appear to have posted the complete code.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

Hi,
I have hopefully fixed the compiling errors, please check and see if it does what you need;
I have changed mode to mode1 as the word "mode" appears to be an allocated C++ variable, if you notice in the IDE it was a different text colour.

const int westPin = 2;        //digital input directions
const int northPin = 6;       //
const int eastPin   = 4;      //
const int southPin = 5;      //
const int mode1Pin = 3;      //  edge detect
const int ledPinOne = 7;    //   digital output
const int ledPinTwo = 8;    //
const int ledPinThree = 14; //
const int ledPinFour  = 15;  //
const int rmotorPin   = 9;    //  analog output
const int dmotorPin   = 11;  //
const int rdirPin        = 10;  //digital output
const int ddirPin        = 12;  //


int westPinState       = 0 ;            //  all variables
int lastWestPinState  = 0;       //
int northPinState        = 0;     //
int lastNorthPinState  = 0;     //
int eastPinState          = 0;    //
int lastEastPinState     = 0;    //
int southPinState        = 0;    //
int lastSouthPinState   = 0;   //
int mode1                   = 0;   //
int val                        = 0;   //
int buttonState           = 0;   //
int mode1State            = 0;   //   all variables


void setup ()   
{
  pinMode(westPin, INPUT);       //   setup input and outputs and turn on internal pullups
  pinMode(northPin, INPUT);      //
  pinMode(eastPin, INPUT);
  pinMode(southPin, INPUT);
  pinMode(mode1Pin, INPUT);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(ledPinOne, OUTPUT);
  pinMode(ledPinTwo, OUTPUT);
  pinMode(ledPinThree, OUTPUT);
  pinMode(ledPinFour, OUTPUT);
  pinMode(rmotorPin, OUTPUT);    //next four pins pertain to the L298 motor driver
  pinMode(dmotorPin, OUTPUT);
  pinMode(rdirPin, OUTPUT);
  pinMode(ddirPin, OUTPUT);
}


void loop()
{
  val = digitalRead(mode1Pin);
  if (val != buttonState && val == LOW)
  {
    mode1++;
  }
  buttonState = val;
  if (mode1State != mode1)
  {
    //if(mode1 != 0)   {
    // }
    switch (mode1)
    {
      case 2:
        digitalWrite(ledPinOne, LOW);
        digitalWrite(ledPinTwo, HIGH);
        digitalWrite(ledPinThree, LOW);
        digitalWrite(ledPinFour, LOW);
        analogWrite(rmotorPin, 150);
        analogWrite(dmotorPin, 100);
        break;
      case 3:
        digitalWrite(ledPinOne, LOW);
        digitalWrite(ledPinTwo, LOW);
        digitalWrite(ledPinThree, HIGH);
        digitalWrite(ledPinFour, LOW);
        analogWrite(rmotorPin, 200);
        analogWrite(dmotorPin, 125);
        break;
      case 4:
        digitalWrite(ledPinOne, LOW);
        digitalWrite(ledPinTwo, LOW);
        digitalWrite(ledPinThree, LOW);
        digitalWrite(ledPinFour, HIGH);
        analogWrite(rmotorPin, 250);
        analogWrite(dmotorPin, 150);
        break;
      default:
        mode1 = 1;
        digitalWrite(ledPinOne, HIGH);
        digitalWrite(ledPinTwo, LOW);
        digitalWrite(ledPinThree, LOW);
        digitalWrite(ledPinFour, LOW);
        analogWrite(rmotorPin, 100);
        analogWrite(dmotorPin, 65);
        break;
    }
    westPinState = digitalRead(westPin);
    northPinState = digitalRead(northPin);
    eastPinState = digitalRead(eastPin);
    southPinState = digitalRead(southPin);


    // compare the buttonstate with the previous state
    if (westPinState != lastWestPinState)
    {
      if (  westPinState == LOW)
      {
        analogWrite(rmotorPin, mode1);
        digitalWrite(rdirPin, LOW);
      }
      else
      {
        analogWrite(rmotorPin, 0);
        digitalWrite(rdirPin, LOW);
      }
    }
    if (northPinState != lastNorthPinState)
    {
      if (northPinState == LOW)
      {
        analogWrite(dmotorPin, mode1);
        digitalWrite(ddirPin, LOW);
      }
      else
      {
        analogWrite(dmotorPin, 0);
        digitalWrite(ddirPin, LOW);
      }
    }
    if (eastPinState != lastEastPinState)
    {
      if (eastPinState == LOW)
      {
        analogWrite(rmotorPin, mode1);
        digitalWrite(rdirPin, HIGH);
      }
      else
      {
        analogWrite(rmotorPin, 0);
        digitalWrite(rdirPin, LOW);
      }
    }
    if (southPinState != lastSouthPinState)
    {
      if (southPinState == LOW)
      {
        analogWrite(dmotorPin, mode1);
        digitalWrite(ddirPin, HIGH);
      }
      else
      {
        analogWrite(dmotorPin, 0);
        digitalWrite(ddirPin, LOW);
      }
    }
    delay(50);
    lastWestPinState = westPinState;
    lastNorthPinState = northPinState;
    lastEastPinState = eastPinState;
    lastSouthPinState = southPinState;
  }
}

Tom... :slight_smile:

First some housekeeping

I noticed that you made some attempts to properly post your code but did not manage; here's how to do it

type
** **[code]** **

paste your code after that
type
** **[/code]** **
after the pasted code

Before pasting your code, use tools -> autoformat in the IDE. It can identify a number of problems

Give a good description of the problem.

===
OK, your code does not compile for starters.

After using auto-format

  pinMode(3, INPUT_PULLUP):
    pinMode(4, INPUT_PULLUP);

The fact that those two don't start at the same position indicates a problem; that colon must be a semi-colon.

After fixing, use auto-format again and next compile again; it will fail on

  pinMode(modePin);

where the argument is missing. Compare it with the line before or after.

After fixing, compile again and it will fail on

  pinMode(rmotor, OUTPUT);    //next four pins pertain to the L298 motor driver

You have declared a rmotorPin and a dmotorPin but you're using rmotor and dmotor.

After fixing that line and the next one, compile again and it will fail on

val = digitalRead(modePin);

Reason is that you placed that at the wrong place. It should have been after the {. Change

void loop()

val = digitalRead(modePin);     {

to

void loop()
{
  val = digitalRead(modePin);

Next you can start fixing all the remaining errors one by one; they all relate to typo mistakes. E.g. ddirPij instead of ddirPin, High instead of HIGH and so on.

The last error is a missing semi-colon after delay(50).

“I am almost seventy years old”

Youngster.

larryd:
“I am almost seventy years old”

Youngster.

I've just been potty trained :smiley: No more nappies, yeah.

The two sets of codes when separated compiled and tested out perfectly but in the edge detection code the motor ran continuously , When I typed in the code I made quite a few mistakes. I wish I knew how to insert the entire code from the IDE into the forum. Should I put the code for the four switches and two motors in front of the edge detection or leave it where it is.

Telescopeman:
I wish I knew how to insert the entire code from the IDE into the forum.

The IDE's Edit/Copy for Forum command is useful here.

Steve

Telescopeman:
I wish I knew how to insert the entire code from the IDE into the forum.

See reply #12, first housekeeping point :wink:

I hope this file will be of some use. it is the complete system for the scope . the smaller schematic is the one in question.