How to program ATmega328P Microcontroller for 2 Axis Control

/* Hello Forum. My challenge is “How to program ATmega328P Microcontroller for 2 Axis Control. Kudos to John Wassar, and others, for sending me here. You can tell by the thread “Then // How do you declare “Then” in this scope?” why He wanted me to do my home work and, come back. He took my garbage and made it compile. I want to transform an Electro-mechanical control into a full blown solid state electronic Plug and Play unit to take to market. This includes a water proof Sensor Unit(s). See below for starters. */ 
boolean switchPin2, switchPin3, switchPin4, switchPin5, switchPin6,  switchPin7;
void setup() {
  unsigned long previousTime = 1520;
  int switchState = 1520;
  int prevSwitchState = 1520;
  int Time = 2;
  long interval = 1;
  for (int x = 2; x < 8; x++) {
    pinMode(x, OUTPUT);
  }
  // int switchPin = pinMode;
  // int then();
  // then == switchPin;
  if (digitalRead(A0) == HIGH)
    switchPin7 = HIGH;
  if (digitalRead(A1) == HIGH)
    switchPin6 = HIGH;
  if (digitalRead(A2) == HIGH)
    switchPin2 = HIGH;
  if (digitalRead(A3) == HIGH)
    switchPin3 = HIGH;
  if (digitalRead(A4) == HIGH)
    switchPin4 = HIGH;
  if (digitalRead(A5) == HIGH)
    switchPin5 = HIGH;
    pinMode(13, OUTPUT);
}
void loop() {
  unsigned long currentTime = millis();
  int Time = 2;
  long interval = 1;
    digitalWrite(15, HIGH);
  delayMicroseconds(150); // Approximately 15% duty cycle @ 1KHz
  digitalWrite(15, LOW);
  delayMicroseconds(1000 - 150);
}

// void setup() // Will this be necessary if the above works OK? Or, will it cause problems if above=Nada
// {
//   pinMode(13, OUTPUT);
// }

// void loop()
// {
//  digitalWrite(15, HIGH);
//  delayMicroseconds(150); // Approximately 15% duty cycle @ 1KHz
//  digitalWrite(15, LOW);
//  delayMicroseconds(1000 - 150);
//  }

/* Please check if the duplicate timing is conflicting, or complimenting, or both necessary, and of course if it will work. I want timing on each output for a small amount of a second, every second and, to include the sensor inputs, with a repeating sampling of the sensors. Using the internal timers would be ideal, and I want to include a GPS chip. */

/If I may, a new question...With the control energized, will the code produce usable output signals?/

First off, it would be really helpful to move the description of what you want to do outside of the code box so that we can read it without having to scroll across.
In this section:

JackOWalden1Sr:

void setup() {

unsigned long previousTime = 1520;
  int switchState = 1520;
  int prevSwitchState = 1520;
  int Time = 2;
  long interval = 1;
  for (int x = 2; x < 8; x++) {
    pinMode(x, OUTPUT);
  }
  // int switchPin = pinMode;
  // int then();
  // then == switchPin;
  if (digitalRead(A0) == HIGH)
    switchPin7 = HIGH;
  if (digitalRead(A1) == HIGH)
    switchPin6 = HIGH;
  if (digitalRead(A2) == HIGH)
    switchPin2 = HIGH;
  if (digitalRead(A3) == HIGH)
    switchPin3 = HIGH;
  if (digitalRead(A4) == HIGH)
    switchPin4 = HIGH;
  if (digitalRead(A5) == HIGH)
    switchPin5 = HIGH;
    pinMode(13, OUTPUT);
}

None of the following are actually used:
unsigned long previousTime = 1520;
int switchState = 1520;
int prevSwitchState = 1520;
int Time = 2;
long interval = 1;

Same sort of thing in your main function:

void loop() {

unsigned long currentTime = millis();
  int Time = 2;
  long interval = 1;
    digitalWrite(15, HIGH);
  delayMicroseconds(150); // Approximately 15% duty cycle @ 1KHz
  digitalWrite(15, LOW);
  delayMicroseconds(1000 - 150);
}

None of the these are used:
unsigned long currentTime = millis();
int Time = 2;
long interval = 1;

Aside from that, your main function will roughly what you want it to do- 15% duty at 1KHz.

JackOWalden1Sr:
Please check if the duplicate timing is conflicting, or complimenting, or both necessary, and of course if it will work. I want timing on each output for a small amount of a second, every second and, to include the sensor inputs, with a repeating sampling of the sensors. Using the internal timers would be ideal, and I want to include a GPS chip. */

I'm not sure what you mean by duplicate timing- it looks like you should be setting up some sort of timing function given the variables you have named, but you aren't actually doing anything with those variables. Your output is only appearing on one pin. No sensor is being sampled, are you referring to the switches that I assume you have connected to switchPin2-7? switchPin2-7 are boolean variables not pins so you can't read them.

Please give some more details on the external hardware you're trying to control, and how you plan to control it. Giving this will help you make more meaningful posts.
It might also help if you put a link to your previous thread that you mentioned at the start of your post.

/*  URL: https://forum.arduino.cc/index.php?board=4.360 
 *   Thread: then//how do you declare "then" in this scope?
 *  Here is what I want my control to do: (Pin assignments)I want output pins 
 *  3 thru 8 to be timed, at HIGH 169; LOW (Delay?) 831 of each second.
 *  (this control can 'almost' perform perfectly by time alone)
 *  (If not, then the sensors come into play)
 *  Analog inputs, A0-A5 are photodiode (IR Receiver) inputs:
 *  A0 is input for output pin 4, designated ledPin?
 *  (this input, when LOW indicates system is on control point)
 *  A1 is input for output pin 5, designated motorPin? WEST direction
 *  A2 is input for output pin 6, designated motorPin? UP direction
 *  A3 is input for output pin 10, designated motorPin? DOWN direction
 *  A4 is input for output pin 11, designated motorPin? EAST direction
 *  (these inputs, A1-A4 when HIGH indicates system is on control point- LOW, motor runs)
 *  A5 is input for output pin 12, designated motorPin? UP direction 
 *  (this is to override pins 6, 10_altitude motor, to keep photocells out of shade)
 *  Analog pin sensor pin values: LOW- motor stop; HIGH- motor run 
 *  (needed is: a way to get timing to the output pins
 *  (also, using two timers would be beneficial- one for WEST, one for UP)
 *  More later (Code) 
*/
 include <Servo(4, 5, 10,11, 12, 13);
void setup() {
  
pinMode(2, OUTPUT);
pinMode(3, OUTPUT); 
pinMode(4, OUTPUT); 
pinMode(5, OUTPUT); 
pinMode(6, OUTPUT); 
pinMode(7, OUTPUT); 
pinMode(8, OUTPUT); 
pinMode(9, OUTPUT);
pinMode(10, OUTPUT); 
pinMode(11, OUTPUT); 
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);

Serial.begin(9600); // put your setup code here, to run once:
  pinMode(LEDpin, OUTPUT);
  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, HIGH);
for int(pinNumber = 2; pinNumber<13; pinNumber++({
  pinMode(pinNumber, OUTPUT); 


}

void loop() {
  // put your main code here, to run repeatedly:
  if A0 == HIGH //do nothing
  if A0 == LOW, digitalWrite (LEDpin, LOW //LED = out

  if output pin5 == LOW, digitalWrite (motorPin5 = HIGH};
  
  if A1 == HIGH //do nothing
  if A1 == LOW, digitalWrite, digitalWrite (motorPin, HIGH //run motor to remove light from sensor 
    if output pin5 == LOW, digitalWrite (motorPin5 = LOW}; // for these, think in terms of reverse logic
    
  if A2 == HIGH //do nothing
  if A2 == LOW, digitalWrite, digitalWrite (motorPin6, HIGH //run motor to remove light from sensor 
   
  if A3 == HIGH //do nothing
  if A3 == LOW, digitalWrite, digitalWrite (motorPin, HIGH //run motor to remove light from sensor 
    if output pin5 == LOW, digitalWrite (motorPin5 = LOW};
    
  if A4 == HIGH //do nothing
  if A4 == LOW, digitalWrite, digitalWrite (motorPin6, HIGH //run motor to remove light from sensor
  
  if A5 == HIGH //do nothing
  if A5 == LOW, digitalWrite, digitalWrite (motorPin, HIGH //run motor to remove light from sensor 
    if output pin5 == LOW, digitalWrite (motorPin5 = LOW};
    
  if A12 == HIGH //do nothing
  if A12 == LOW, digitalWrite, digitalWrite (motorPin6, HIGH //run motor to remove light from sensor
   
  if A13 == LOW, digitalWrite, digitalWrite (motorPin, HIGH //run motor to Park Platform in the East  
  if A13 == LOW, digitalWrite, digitalWrite (motorPin, LOW //run motor to Park Platform in the East //Check the attached pdf for info on where I'm at with Arrow Electronics// 
  }
}

ATmega328P-Microcontroller.pdf (103 KB)

[code #include <Servo.h>
const int motorPin (4, 5, 10,11, 12, 13);
const int LEDpin; 
const int switchPin; 
void setup() {
  
pinMode(2, OUTPUT);
pinMode(3, OUTPUT); 
pinMode(4, OUTPUT); 
pinMode(5, OUTPUT); 
pinMode(6, OUTPUT); 
pinMode(7, OUTPUT); 
pinMode(8, OUTPUT); 
pinMode(9, OUTPUT);
pinMode(10, OUTPUT); 
pinMode(11, OUTPUT); 
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);

Serial.begin(9600); // put your setup code here, to run once:
  pinMode(LEDpin, OUTPUT);
  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, HIGH);
for (int pinNumber = 2; pinNumber<13; pinNumber++){
  pinMode(pinNumber, OUTPUT); / is this looking better?/]
 #include <Servo.h>
const int motorPin (4, 5, 10,11, 12, 13);
const int LEDpin; 
const int switchPin; 
void setup() {
  
pinMode(2, OUTPUT);
pinMode(3, OUTPUT); 
pinMode(4, OUTPUT); 
pinMode(5, OUTPUT); 
pinMode(6, OUTPUT); 
pinMode(7, OUTPUT); 
pinMode(8, OUTPUT); 
pinMode(9, OUTPUT);
pinMode(10, OUTPUT); 
pinMode(11, OUTPUT); 
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);

Serial.begin(9600); // put your setup code here, to run once:
  pinMode(LEDpin, OUTPUT);
  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, HIGH);

}

void loop(){
  // put your main code here, to run repeatedly
  pinMode (LEDpin, OUTPUT);
  if (A0 == HIGH); //do nothing
  if (A0 == LOW); digitalWrite (LEDpin, LOW); //LED = out)


  if (A1 == HIGH); //do nothing
  if (A1 == LOW); digitalWrite (motorPin, HIGH); //run motor to remove light from sensor 
 
  
  if (A2 == HIGH); //do nothing  
  if (A2 == LOW); digitalWrite (motorPin, HIGH); //run motor to remove light from sensor 
   
  if (A3 == HIGH); //do nothing
  if (A3 == LOW); digitalWrite (motorPin, HIGH); //run motor to remove light from sensor 

  if (A4 == HIGH); //do nothing
  if (A4 == LOW); digitalWrite (motorPin, HIGH); //run motor to remove light from sensor
  
  if (A5 == HIGH); //do nothing
  if (A5 == LOW); digitalWrite (motorPin, HIGH); //run motor to remove light from sensor 
    }

//* This compiles but, will it do anything constructive? If not, why not? *//

 // Do over; after all, it's My Code. 
// The solar plarform must first be aimed at the sun. (Sunbeam must be shining on face sensor- (A0). 
// Please advise as to what wont work. 
boolean switchPin2, switchPin3, switchPin4, switchPin5, switchPin6, switchPin7, switchPin8, switchPin9, switchPin10, switchPin11, switchPin12;
void setup() {
   pinMode(13, OUTPUT);
  int x = 2;
}

void loop() {
  unsigned long previousTime = 1000;
  int switchState = 152;
  int prevSwitchState = 848;
  int Time = 2;
  long interval = 1;
  for (int x = 2; x < 8; x++);
  int x = 2;
    pinMode(x, OUTPUT);
  int switchPin = pinMode;
  int then();
  then == switchPin;
  if (digitalRead(A0) == HIGH) // This indicates sunlight is playing on sensor. 
    switchPin3 = HIGH; // This indicates the system is 'On Control'.
  if (digitalRead(A1) == LOW) // This indicates no light on sensor, and is on control. 
    switchPin5 = LOW; // Ditto- on control. 
  if (digitalRead(A2) == LOW)
    switchPin5 = HIGH; // This indicates switchState = 152 to keep on control. 
    switchPin5 = LOW; // This indicates else, delay 848; 
  if (digitalRead(A3) == LOW) //This indicates on control. 
    switchPin6 = LOW; // This indicates on control. 
    switchPin6 = HIGH; // This indicates switchState = 152 to keep on control.
  if (digitalRead(A4) == LOW) // This indicates on control. 
    switchPin6 = LOW; // This indicates else, delay 848;
  if (digitalRead(A4) == HIGH); // This indicates switchState = 152 to keep on control. 
  } 

  unsigned long currentTime = millis();
  int Time = 2;
  long interval = 1;

Jack, you can't just put up a block of code with no explanation and expect help. You need to give us some more information- what you're trying to do, how you're trying to do it, what undesired results you are seeing. In one of my previous replies I put a link to a sticky post that explains a good way to ask for help on these forums- please read it. I'm bringing this up because you're not going to get any help if you keep going the way you have been.
With regards to your code- I think you need to go back to basics and start learning from scratch. The code you've posted is mostly nonsense. There are plenty of guides on getting started with Arduino. Just as a guide, it looks like you'll need to learn how to read digital inputs (from the sensors) and how to drive digital outputs (using the digitalWrite command to drive some motors).
Good luck, and if you have specific questions then always feel free to ask.

/* Hello BJHenry, and all, thanks for your input and patience. Sorry for being in such a hurry. //
// Normally, I am a very patient person; here is what I want to do: //
// Am using an Arduino Uno board, with ATMega 328P-PU Microcontroller. //
// I want to track the sun from sun up to sun down, 365 days per year, including through the clouds. //
// I want to track by time, & sensor inputs, utilizing 2 bidirectional motors; 1 ea. for altitude - azimuth. //
// I believe it can be done by time alone but, if not, then the sensors come into play. //
// Start by driving the West motor 169 parts out of 1000 per second, i.e. millis, 169000 on– 831000 off//
// Then by driving the Up motor 169 parts out of 1000 per second, i.e. millis, 169000 on– 831000 off. //
// Preferably, with 2 internal timers, on output pins 10, 11 to allow for separate minor adjustments. //
// (Ultimately, I want to utilize GPS timing (on 3rd)? so as to locate, and operate anywhere on Earth.) //
// Let’s assume using inputs A0- A5 for 6 IR sensors, and using digitalWrite on pins 5- 8, 10, 11, 13. //
// Try page 38 of URL: Programming Questions - Arduino Forum for previous thread. //
// So as to get a good idea of where I’m coming from, please see the attached drawing. //
// I will post some code later; thanks again, and have a nice day. // */

ATmega328P-Microcontroller.pdf (103 KB)

What sort of motor are you planning to use? The fact that you want to drive it by switching the driving output on and off for a specific amount of each second makes me think that you want to use a servo motor- if that is the case then there is a library that can take care of that better than bit-banging using digitalWrite() and delay().

//I'm really curious to know why
//you post everything as
//comments

// Try page 38 of URL: Programming Questions - Arduino Forum for previous thread. //

That's an odd way of pointing to a thread. First, pages on a board are very difficult to get to with the single page forward arrows* and second if some one reads this thread in a few weeks' time, your old thread will be page who-knows-what.

(*Unless there's a jump-to-page X that I haven't seen?)

 // I'm short on everything after arithmetic, and computer programming. //
// Try searching maximum pages, now page 40, then 1 at a time - I know of no other way. //
/* Separately, these appeared to work; when cut and pasted, the following error presented this:
(at void loop) a function-definition is not allowed here before '{' token
(red text, in black) exit status 1
a function-definition is not allowed here before '{' token */

int mySensVals[6] = [A0, A1, A2, A3, A4, A5]{;
boolean digitalRead2, digitalRead3, digitalRead4, digitalRead5, digitalRead6, digitalRead7, digitalRead8, digitalRead9, digitalRead10, digitalRead11, digitalRead12, digitalRead13;
pinMode; digitalRead(A0) INPUT_PULLUP 

const int motorPin (2, 3, 4, 5, 6, 7, 8, 9, 10,11, 12, 13);
int switchPin = digitalWrite;
 
void setup() {
digitalWrite(2, OUTPUT);
digitalWrite(3, OUTPUT); 
digitalWrite(4, OUTPUT); 
digitalWrite(5, OUTPUT); 
digitalWrite(6, OUTPUT); 
digitalWrite(7, OUTPUT); 
digitalWrite(8, OUTPUT); 
digitalWrite(9, OUTPUT);
digitalWrite(10, OUTPUT); 
digitalWrite(11, OUTPUT); 
digitalWrite(12, OUTPUT);
digitalWrite(13, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  unsigned long previousTime = 1000000;
  int switchState = 152000;
  int prevSwitchState = 848000;
  int Time = 2;
  long interval = 1;
  for (int x = 2; x < 8; x++);
  int x = 2;
    pinMode(x, OUTPUT);
  int digitalWrite = pinMode;
  int then();
  then == digitalWrite;
  if (digitalRead(A0) == HIGH) // This indicates sunlight is playing on sensor. 
    digitalWrite3 = HIGH; // This led indicates the system is 'On Control'.
  if (digitalRead(A1) == LOW) // This indicates no light on sensor, and is on control. 
    digitalWrite5 = LOW; // Ditto- on control. 
    digitalWrite5 = HIGH; // Indicates motor runs to bring back on control. 
  if (digitalRead(A2) == LOW)
    digitalWrite6 = HIGH; // This indicates switchState = 152000 to keep on control. 
    digitalWrite6 = LOW; // This indicates else, delay 848000; 
  if (digitalRead(A3) == LOW) //This indicates on control. 
    digitalWrite7 = LOW; // This indicates on control. 
    digitalWrite7 = HIGH; // This indicates switchState = 152000 to keep on control.
  if (digitalRead(A4) == LOW) // This indicates on control. 
    digitalWrite8 = LOW; // This indicates else, delay 848000;
    digitalWrite8 = HIGH; // Indicates motor runs to bring back on control. 
  if (digitalRead(A5) == HIGH); // This indicates switchState = 152000 to keep on control. 
    digitalWrite9 = LOW; // This indicates else, delay 848000; 
    digitalWrite9 = HIGH; // Indicates motor runs to bring back on control. }
} 

}

Jack, for the sake of those of us trying to read your posts- don't put the non-code portion of your post inside code tags. It makes it harder to read.

As for your code- you are obviously in over your head trying to do everything that you want to do. I don't mean that as an insult- the best projects will always push the boundaries of your knowledge and everyone starts off knowing nothing.
This forum isn't going to write your code for you but we will help you learn how to do what you want to do. You'll still have to do the learning yourself though.
For a project like this where you are going to be learning as you go, the best way to get started is to break the project down into the simplest parts possible. Going by the little information you've given us your project could be broken down into a couple of steps:

  1. Read an input
  2. Depending on the state of the input, drive an output.

The Arduino IDE has example sketches that cover both of these topics. Open the IDE then navigate to File>Examples>Digital>Button. This will show you how to setup a digital input (like it seems you want to do for the sun sensors) and read that input to do something. In the case of the example it turns an LED on or off, but in your case the next step could be to control a motor.

You really need to stop, take a breath, and go back to first principles, because you're basically just making stuff up:

  int switchPin = pinMode;
  int then();
  then == switchPin;

// Try searching maximum pages, now page 40, then 1 at a time - I know of no other way. //

If that was at me, here's how to link to a thread. Open the thread you want to be able to link to, then select and ctrl-C its URL. Then in the post you want to link from, select the text in your post you want to use as the clickable link, and hit the "chain" icon. Ctrl-V your previously ctrl-C'd url in where it says "Enter URL", and hit OK there and on the next panel.

So for example you can now click here to go to one of your other threads, which may or may not be the one you were talking about because I cba to track that one down.

/* The below code is in the public domain. With the controller board powered up,
with 5 volts, will allow for the uploading of code to the board via computer.
The below 'serial' code will now be in memory, and will light the internal led?
// kenwood120s, I'm sorry for not mentioning your name, and I know not how to
re-open a closed thread
// Keep in mind the pull-up means the pushbutton's logic is inverted. It goes
// HIGH when it's open, and LOW when it's pressed. Turn on pin 13 when the
// button's pressed, and off when it's not:
The additional code is what I want to do:
// When digitalWrite(5, LOW); is low,- means 'system is not 'on control'
// System must be manually aimed at the sun, digitalRead(A5, LOW); must be 'HIGH'
// When digitalWrite(6, HIGH); is high, motor is energized- means 'off control'
// When digitalWrite(6, LOW); is low, motor is deenergized- means 'on control'
// etc.. please indicate, yes or no, if I'm on the right path. It compiles?
// Why sensorVal seems to work, when digitalRead don't?
// Why error "exit status 1 expected '}' at end of input */

void setup() {
//start serial connection
Serial.begin(9600);
//configure pin 2 as an input and enable the internal pull-up resistor
pinMode(2, INPUT_PULLUP);
pinMode(13, OUTPUT);
}

void loop() {
//read the pushbutton value into a variable
int sensorVal = digitalRead(2);
//print out the value of the pushbutton
Serial.println(sensorVal);
//print out the value of the input sensor
int digitalRead(A0);

if (sensorVal == HIGH) {
digitalWrite(13, LOW);
} else {
digitalWrite(13, HIGH);

if (sensorVal, A0 == HIGH) {
digitalWrite(5, LOW);
} else {
digitalWrite(5, LOW);

if (sensorVal, A1 == HIGH) {
digitalWrite(6, HIGH);
} else {
digitalWrite(6, LOW);
}

// Why sensorVal seems to work, when digitalRead don't?

Because this:

 int sensorVal = digitalRead(2);

if (sensorVal == HIGH) {
  digitalWrite(13, LOW);
} else {
  digitalWrite(13, HIGH);

is different to this:

 if (sensorVal, A0 == HIGH) {

digitalWrite(5, LOW);
} else {
  digitalWrite(5, LOW);

Why error "exit status 1 expected '}' at end of input */

Because you're missing several closing braces.

JackOWalden1Sr:
I know not how to re-open a closed thread

Threads typically don't get closed, but what's closed threads got to do with this? I merely suggested a way to link to an existing thread from a post, in a way that's better than saying it's on page X of a forum, since it won't be on that page for long.

PLEASE us code tags: you have used them before so you know how, but just check you don't include your post inside the tags.

//Posting as comments like this is crazy.

(And in any case the //s are redundant since you use the /.../ approach :wink: )

Thanks BJHenry, now I’m getting the hang of opening and closing braces; and how to do it automatically, with a quick ‘enter’. Also, thanks kenwood120s, for your good input. :slight_smile: