Issues with Arduino conversion

So I have completed my project code. My end result has always been to output the light flashing as a basic on-off signal to a computer that I use for work. I am running into a little problem in that the sequence is not constant, in that if I am running at the fastest on-off sequence possible for "Pump 2" as an example, which is 40 millis it isn't always 40 millis which causes the on-off sequence to appear to speed up and slow down. I can see it on the flashing light that I still have on the breadboard I scaled my project to not use an arduino Uno but to only use a ATMEGA328. If I go back to using the Arduino Uno it works properly so it doesnt seem to be the code that is incorrect. I'm not sure what I am doing wrong. My power supply outputs 5v at 2A. I have included my fritzing document. So apparently I can't upload my fritzing document but I used the following website to make the ATMEGA328 “board”.

https://dronebotworkshop.com/arduino-uno-atmega328/

Any ideas on why this isn’t working properly?

I won't look at any more fritzing diagrams and I am not going to dig through a big article to find a schematic. Good Luck and goodbye.

Perhaps if you posted the code and your circuit diagram, some of us could take a look. However, with nothing to go on, there is no point in anyone commenting. Remember the code tags for posting your code.

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

AJLElectronics:
Perhaps if you posted the code and your circuit diagram, some of us could take a look. However, with nothing to go on, there is no point in anyone commenting. Remember the code tags for posting your code.

It says that my post is too long when I try to post the code so I didn't post it because I didn't think it was necessary as the project works on the arduino just not on the "permanent" one. I will break it up and see if it helps.

// put your setup code here, to run once:
const int joystickInput = A0;
const int depthProxAPin = 12;
const int depthProxBPin = 11;
const int adrSwitch = 5;
const int adrControl = A1;
const int rotarySwitch = 4;
const int rotaryControl = A2;
const int rotaryProxPin = 10;
const int pump1Switch = 3;
const int pump1Control = A3;
const int pump1ProxPin = 9;
const int pump2Switch = 2;
const int pump2Control = A4;
const int pump2ProxPin = 8;

int adrButtonState = LOW, adrPrevButtonState;
int rotaryButtonState = LOW, rotaryPrevButtonState;
int pump1ButtonState = LOW, pump1PrevButtonState;
int pump2ButtonState = LOW, pump2PrevButtonState;


enum { NEUTRAL, UP, DOWN };
enum { ON, OFF};
int dir = NEUTRAL;
int adrSw = OFF;
int rotarySw = OFF;
int pump1Sw = OFF;
int pump2Sw = OFF;
int blockState;
int rotaryState;
int pump1State;
int pump2State;

unsigned long currentDelay;
unsigned long adrSpeedDelay;
unsigned long rotarySpeedDelay;
unsigned long pump1SpeedDelay;
unsigned long pump2SpeedDelay;
unsigned long blockStartTime;
unsigned long rotaryStartTime;
unsigned long pump1StartTime;
unsigned long pump2StartTime;


void setup() {

  // designate depthProxAPin and depthProxBPin as an OUTPUT pin for the LED
  pinMode (depthProxAPin, OUTPUT);
  pinMode (depthProxBPin, OUTPUT);
  pinMode (rotaryProxPin, OUTPUT);
  pinMode (pump1ProxPin, OUTPUT);
  pinMode (pump2ProxPin, OUTPUT);

  // designate joystickInput as an Analog INPUT for the potentiometer
  pinMode (joystickInput, INPUT);

  Serial.begin(9600);

  adrPrevButtonState = digitalRead(adrSwitch);
  rotaryPrevButtonState = digitalRead(rotarySwitch);
  pump1PrevButtonState = digitalRead(pump1Switch);
  pump2PrevButtonState = digitalRead(pump2Switch);
  
}

void loop()
{
  adrLoop();
  rotaryLoop();
  pump1Loop();
  pump2Loop();
}


void blocks() {

  unsigned long currentTime = millis();

  int potValue = analogRead(joystickInput);
  int outputValue = map(potValue, 64, 912, 100, -100);  //map full up to 100 and full down to -100

  Serial.println(outputValue);

  if (outputValue > 10) {
    //do this when joystick is pressed from the neutral position up
    currentDelay = map(outputValue, 10, 100, 500, 1);  //map the delay progressivly less as joystick is moved from neutral to full up
    if (dir != UP) {
      // first time joystick went up from neutral or from down
      dir = UP;
      blockState = 0;
      blockStartTime = currentTime;
      digitalWrite(depthProxAPin, HIGH);
    }
    else {
      // joystick has been up so see if time to move to next state
      if ( currentTime - blockStartTime >= currentDelay ) {
        blockState++;
        switch (blockState) {
          case 1:
            digitalWrite(depthProxBPin, HIGH);
            break;
          case 2:
            digitalWrite(depthProxAPin, LOW);
            break;
          case 3:
            digitalWrite(depthProxBPin, LOW);
            break;
          case 4:
            digitalWrite(depthProxAPin, HIGH );
            blockState = 0;  // last state so roll over to zero
            break;
        }
        blockStartTime = currentTime;
      }
    }
  }
  else if (outputValue < -10) {
    //do this when joystick is pressed from the neutral position down
    currentDelay = map(outputValue, -10, -100, 500, 1);  //map the delay progressivly less as joystick is moved from neutral to full down
    if (dir != DOWN) {
      // first time joystick went down from neutral or from up
      dir = DOWN;
      blockState = 0;
      blockStartTime = currentTime;
      digitalWrite(depthProxBPin, HIGH);
    }
    else {
      // joystick has been up so see if time to move to next state
      if ( currentTime - blockStartTime >= currentDelay ) {
        blockState++;
        switch (blockState) {
          case 1:
            digitalWrite(depthProxAPin, HIGH);
            break;
          case 2:
            digitalWrite(depthProxBPin, LOW);
            break;
          case 3:
            digitalWrite(depthProxAPin, LOW);
            break;
          case 4:
            digitalWrite(depthProxBPin, HIGH );
            blockState = 0;  // last state so roll over to zero
            break;
        }
        blockStartTime = currentTime;
      }
    }
  }
  else {
    //do this when joystick is in neutral position
    dir = NEUTRAL;
    digitalWrite (depthProxBPin, LOW);
    digitalWrite (depthProxAPin, LOW);
  }
}


void adr()
{
  int adrSpeed = analogRead(adrControl);
  unsigned long adrTime = millis();

  adrSpeedDelay = map(adrSpeed, 124, 931, 500, 40);  //map the delay progressivly less as joystick is moved from neutral to full up
  if ( adrTime - blockStartTime >= adrSpeedDelay )
  {
    blockState++;
    switch (blockState)
    {
      case 1:
        digitalWrite(depthProxAPin, HIGH);
        break;
      case 2:
        digitalWrite(depthProxBPin, LOW);
        break;
      case 3:
        digitalWrite(depthProxAPin, LOW);
        break;
      case 4:
        digitalWrite(depthProxBPin, HIGH );
        blockState = 0;  // last state so roll over to zero
        break;
    }
    blockStartTime = adrTime;
  }
}
void rotary()
{
  int rotarySpeed = analogRead(rotaryControl);
  unsigned long rotaryTime = millis();

  rotarySpeedDelay = map(rotarySpeed, 124, 931, 500, 40);  //map the delay progressivly less as joystick is moved from neutral to full up
  if ( rotaryTime - rotaryStartTime >= rotarySpeedDelay )
  {
    rotaryState++;
    switch (rotaryState)
    {
      case 1:
        digitalWrite(rotaryProxPin, HIGH);
        break;
      case 2:
        digitalWrite(rotaryProxPin, LOW);
        rotaryState = 0;  // last state so roll over to zero
        break;
    }
    rotaryStartTime = rotaryTime;
  }
}

void pump1()
{
  int pump1Speed = analogRead(pump1Control);
  unsigned long pump1Time = millis();

  pump1SpeedDelay = map(pump1Speed, 124, 931, 500, 40);  //map the delay progressivly less as joystick is moved from neutral to full up
  if ( pump1Time - pump1StartTime >= pump1SpeedDelay )
  {
    pump1State++;
    switch (pump1State)
    {
      case 1:
        digitalWrite(pump1ProxPin, HIGH);
        break;
      case 2:
        digitalWrite(pump1ProxPin, LOW);
        pump1State = 0;  // last state so roll over to zero
        break;
    }
    pump1StartTime = pump1Time;
  }
}

void pump2()
{
  int pump2Speed = analogRead(pump2Control);
  unsigned long pump2Time = millis();

  pump2SpeedDelay = map(pump2Speed, 0, 1020, 700, 60);  //map the delay progressivly less as joystick is moved from neutral to full up
  if ( pump2Time - pump2StartTime >= pump2SpeedDelay )
  {
    pump2State++;
    switch (pump2State)
    {
      case 1:
        digitalWrite(pump2ProxPin, HIGH);
        break;
      case 2:
        digitalWrite(pump2ProxPin, LOW);
        pump2State = 0;  // last state so roll over to zero
        break;
    }
    pump2StartTime = pump2Time;
  }
}

void adrLoop()
{
  
  adrButtonState = digitalRead(adrSwitch);
  
  if ( adrButtonState != adrPrevButtonState ) // adrSwitch has changed, react
  {
    if ( adrButtonState == HIGH ) //adrSwitch was just turned on
    {
      adrSw = ON;
      rotaryState = 0;
      rotaryStartTime = millis();
      digitalWrite (depthProxBPin, HIGH);
    }
    else  //adrSwitch was just turned off
    {
      adrSw = OFF;
      digitalWrite (depthProxBPin, LOW);
      digitalWrite (depthProxAPin, LOW);
    }
    delay(50); //debounce adrSwitch
    adrPrevButtonState = adrButtonState;
  }
  
  if ( adrSw == OFF ) 
  {
    blocks();
  }
  else 
  {
    adr();
  }
}

void rotaryLoop()
{

  rotaryButtonState = digitalRead(rotarySwitch);
  
  if ( rotaryButtonState != rotaryPrevButtonState ) // rotarySwitch has changed, react
  {
    if ( rotaryButtonState == HIGH ) //rotarySwitch was just turned on
    {
      rotarySw = ON;
      rotaryState = 0;
      rotaryStartTime = millis();
      digitalWrite (rotaryProxPin, HIGH);
    }
    else  //adrSwitch was just turned off
    {
      rotarySw = OFF;
      digitalWrite (rotaryProxPin, LOW);
    }
    delay(50); //debounce rotarySwitch
    rotaryPrevButtonState = rotaryButtonState;
  }
  
  if ( rotarySw == OFF ) 
  {
    digitalWrite (rotaryProxPin, LOW);
  }
  else 
  {
    rotary();
  }
}

void pump1Loop()
{
  
  pump1ButtonState = digitalRead(pump1Switch);
  
  if ( pump1ButtonState != pump1PrevButtonState ) // pump1Switch has changed, react
  {
    if ( pump1ButtonState == HIGH ) //pump1Switch was just turned on
    {
      pump1Sw = ON;
      pump1State = 0;
      pump1StartTime = millis();
      digitalWrite (pump1ProxPin, HIGH);
    }
    else  //adrSwitch was just turned off
    {
      pump1Sw = OFF;
      digitalWrite (pump1ProxPin, LOW);
    }
    delay(50); //debounce pump1Switch
    pump1PrevButtonState = pump1ButtonState;
  }
  
  if ( pump1Sw == OFF ) 
  {
    digitalWrite (pump1ProxPin, LOW);
  }
  else 
  {
    pump1();
  }
}

void pump2Loop()
{
  
  pump2ButtonState = digitalRead(pump2Switch);
  
  if ( pump2ButtonState != pump2PrevButtonState ) // pump2Switch has changed, react
  {
    if ( pump2ButtonState == HIGH ) //pump2Switch was just turned on
    {
      pump2Sw = ON;
      pump2State = 0;
      pump2StartTime = millis();
      digitalWrite (pump2ProxPin, HIGH);
    }
    else  //adrSwitch was just turned off
    {
      pump2Sw = OFF;
      digitalWrite (pump2ProxPin, LOW);
    }
    delay(50); //debounce pump2Switch
    pump2PrevButtonState = pump2ButtonState;
  }
  
  if ( pump2Sw == OFF ) 
  {
    digitalWrite (pump2ProxPin, LOW);
  }
  else 
  {
    pump2();
  }
}

This is a jpeg export of the fritzing document.

We need a circuit diagram. Fritzing pictures will just put people off responding to you. You can attach your complete .ino code if it is to big to list in code tags.

Fritzing diagrams are intended as simplified build instructions for those building stuff on breadboards that dont understand circuits or schematics.

If you want experienced people to tell you why a circuit may not work, publish a schematic, an image of a pencil and paper drawing is fine.

srnet:
Fritzing diagrams are intended as simplified build instructions for those building stuff on breadboards that dont understand circuits or schematics.

If you want experienced people to tell you why a circuit may not work, publish a schematic, an image of a pencil and paper drawing is fine.

Sorry, I'm rather inexperienced with the whole Arduino thing anyways. I used the Fritz to create a schematic and also included the schematic that was provided on dronebotworkshop to create the standalone Arduino. Hope this is what you are looking for.

Regards,

Well it's close, but no cigar. A proper circuit diagram looks something like this, although not a wonderful example, you will see how it is laid out logically and without litle zigzags on pinouts.:

Both of those diagrams are missing the required 0.1uF decoupling caps on the power supply pins. Vcc to ground and AVcc to ground.

I hope that your project is not actually wired like the fritzing shows. Sorry, but there is no way that anyone could build a working circuit from that diagram. For instance, look at where pins 2 and 3 or 10 and 11 go.

groundFungus:
Both of those diagrams are missing the required 0.1uF decoupling caps on the power supply pins. Vcc to ground and AVcc to ground.

Not sure I understand where they go but I used a 10uF capacitor tied into the VCC and AVCC.

mpboyer3496:
Not sure I understand where they go but I used a 10uF capacitor tied into the VCC and AVCC.

You need a 100nF (0.1uF) across the rails at each active device, as close as possible to the device pins. It is there to handle high frequency noise.

AJLElectronics:
You need a 100nF (0.1uF) across the rails at each active device, as close as possible to the device pins. It is there to handle high frequency noise.

So like I did in the attached schematic?

Those caps across all the terminal blocks may or may not be required. We have no way of knowing since you haven’t told us what’s connected to them and since getting a usable schematic appears difficult, how about you show us what your creation looks like. A few decent, in focus photos are worth thousands of words.

How did you have things built such that worked with an Uno? It would help if we knew more about what is attached to all those terminals and a much better description of how the project is failing. How you you know it’s not 40 milliseconds? Do you have a scope?

We are happy to help but so far, we lack the required information to help you help yourself.

I connected everything to the uno and had no issues, when I connect the same wires to the breadboard locations on the ATMEGA328 as they relate to the Uno they don’t work properly. As far as a schematic is involved it’s the best I can do as I never drew one up. I’m out of town so I can’t get pictures and won’t be able to for a few weeks. I was hopeful that there is some difference between the uno and the ATMEGA328 “uno” I built.

As far as what they connect to, the outputs connect to a proprietary junction box that looks for an on-off signal, my issue is that the ATMEGA328 does not output the on-off signal steady.

There are numerous differences between an Uno and an atMega328 on a breadboard. The most obvious ones, as they relate to your project, are 1) power supply 2) bypass capacitors 3) reset circuitry.

  1. How are you powering the breadboard?

  2. You absolutely need a 100nf ceramic capacitors across the VCC/gnd and AVCC/gnd pins of the processor directly on the processor pins, not connected with jumpers off on a corner of the breadboard somewhere. A single 10uf capacitor is not better because it’s a hundred times larger in value.

  3. Why is the reset pin being brought out to a terminal strip? That factor alone could be causing spurious resets of the processor.

On a side note, the led on pin 13 cannot work as shown, it’s backwards. As shown with the anode connected to pin D13 through the 330 ohm resistor, the cathode must be grounded in order to illuminate when D13 is high. If you want it to illuminate with D13 low, connect anode to VCC and cathode to 330 ohm then to D13. Cathode is the lead with the flat side.

Start by fixing the led and use the blink sketch to insure the processor runs properly.

Anomalies with bypassing and the reset pin will many times be obvious with the blink sketch misbehaving.

WattsThat:
There are numerous differences between an Uno and an atMega328 on a breadboard. The most obvious ones, as they relate to your project, are 1) power supply 2) bypass capacitors 3) reset circuitry.

  1. How are you powering the breadboard?

  2. You absolutely need a 100nf ceramic capacitors across the VCC/gnd and AVCC/gnd pins of the processor directly on the processor pins, not connected with jumpers off on a corner of the breadboard somewhere. A single 10uf capacitor is not better because it’s a hundred times larger in value.

  3. Why is the reset pin being brought out to a terminal strip? That factor alone could be causing spurious resets of the processor.

On a side note, the led on pin 13 cannot work as shown, it’s backwards. As shown with the anode connected to pin D13 through the 330 ohm resistor, the cathode must be grounded in order to illuminate when D13 is high. If you want it to illuminate with D13 low, connect anode to VCC and cathode to 330 ohm then to D13. Cathode is the lead with the flat side.

Start by fixing the led and use the blink sketch to insure the processor runs properly.

Anomalies with bypassing and the reset pin will many times be obvious with the blink sketch misbehaving.

  1. With a 120VAC to 5VDC power supply
  2. So remove the 10uF capacitor and install 2 100nF capacitors, one from pin 20 to ground and one from pin 7 to ground?
  3. I have a 10 kOhm resistor from pin 1 to ground.
  4. I screwed up when I created the schematic for the LED, it works properly I just need to fix it on the schematic.