Hi guys,
First of all dont blame me for my english writing skills. its not my native language. thx
So... I am currently doing my diploma project.
Therefore i am writing a program for my arduino-board which should controll two motors in the end.
So far the calculations in my program and serial communication works fine.
The only problem i have is that a calculated value i want to write on a pin (analog) does not work.
But it only does not work with the calculated value (pins is always LOW) tho the value is not 0.
First i thought the problem is that the value is not declared as volatile... changed that without any better results
Now i think it has something to do with pointers.
// configuration of engine control
// via ISR using CTC - every 1 ms read/write
// ~Bachler Sebastian~
// include
#include <avr/interrupt.h>
// values
struct values{
volatile int sensval_atm; // sensorvalue at the moment
volatile int sensval_old; // sensorvalue one cyclus before
volatile int sensvalDif_atm; // difference between sensorvariable old and atm at the moment
volatile int sensvalDif_old; // difference between sensorvariable old and atm one cyclus before
volatile int outputval; // digital ouput value (allready considering aceleration of potentiometer)
volatile float multiplier; // factor rising/lowering the resistance
volatile int power; // current motor power
volatile int angle; // current vertical angle
volatile float velocity; // pull velocity
volatile float acceleration; // pull acceleration
volatile float frequency; // pull-frequency in time between each pull
// input pins
const int horizontal; // horizontal potentiometer input = angle horizontal
const int vertical; // vertical potentiometer input = angle vertical
// output pins
const int idle; // select the pin for the (red LED) - idling
const int pull; // select the pin for the (green LED) - pulling
// enable pins
const int ena;
// identifier
const char ident;
// variables too choose if printing data is necessary
volatile int power_old ;
volatile float angle_old;
volatile float horizontal_old;
volatile float velocity_old;
volatile float acceleration_old;
volatile float frequency_old;
// variable needed because YEAH
boolean pulled;
};
// initialisation
values left = {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, A7, A6, 2, 3, 6, 'a', 0, 0, 0, 0, 0, 0, false}; // values left
values right = {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, A5, A4, 4, 5, 7, 'A', 0, 0, 0, 0, 0, 0, false}; // values right
// engine control
void ec(struct values & val){
// enable
digitalWrite(val.ena , HIGH);
// sensor input
val.sensval_atm = analogRead(val.horizontal);
val.angle = analogRead(val.vertical);
val.multiplier = val.multiplier + analogRead(A3)/1000;
// calculations
val.sensvalDif_atm = val.sensval_old - val.sensval_atm;
val.acceleration = val.sensvalDif_atm - val.sensvalDif_old;
// motor off
if(val.sensval_atm > val.sensval_old || val.angle > 40) {
val.outputval = 0;
Serial.println(val.outputval);
analogWrite(val.pull , val.outputval);
digitalWrite(val.idle , LOW);
val.frequency = millis();
if(val.pulled == true) {
val.frequency = millis() - val.frequency;
val.pulled = false;
}
}
// motor on
if(val.sensval_atm < val.sensval_old && val.angle < 40) {
val.pulled = true;
digitalWrite(val.idle, LOW);
// acceleration +
if(val.sensvalDif_old < val.sensvalDif_atm) {
val.outputval += val.acceleration*val.multiplier;
Serial.println(val.outputval);
val.outputval = 20;
analogWrite(val.pull, val.outputval);
}
// acceleration -
else {
if(val.sensvalDif_old > val.sensvalDif_atm) {
val.outputval += val.acceleration*val.multiplier;
if(val.outputval < 0) val.outputval = 0;
Serial.println(val.outputval);
analogWrite(val.pull, val.outputval);
}
// constant movement
else {
Serial.println(val.outputval);
analogWrite(val.pull, val.outputval);
}
}
}
//---[DEBUGGING]---
Serial.println(left.outputval);
//---[DEBUGGING]---
// set atm-values to old-values for next cyclus
val.sensval_old = val.sensval_atm;
val.sensvalDif_old = val.sensvalDif_atm;
}
void echo(struct values & val){
// serial output
if(val.power != val.power_old)
{
char ident = val.ident;
Serial.print(ident);
Serial.print(val.power); // power (0-255)
val.power_old = val.power;
}
if(val.frequency != val.frequency_old)
{
char ident = val.ident+1;
Serial.print(ident);
Serial.print(val.frequency); // frequency (time between each pull)
val.frequency_old = val.frequency;
}
if(val.angle != val.angle_old)
{
char ident = val.ident+2;
Serial.print(ident);
Serial.print(val.angle); // vertical angle (0-1023)
val.angle_old = val.angle;
}
if(val.sensval_atm != val.horizontal_old)
{
char ident = val.ident+3;
Serial.print(ident);
Serial.print(val.sensval_atm); // horizontal angle (0-1023)
val.horizontal_old = val.sensval_atm;
}
if(val.sensvalDif_atm != val.velocity_old)
{
char ident = val.ident+4;
Serial.print(ident);
Serial.print(val.sensvalDif_atm); // horizontal speed (0-1023)
val.velocity_old = val.sensvalDif_atm;
}
if(val.acceleration != val.acceleration_old)
{
char ident = val.ident+5;
Serial.print(ident);
Serial.print(val.acceleration); // horizontal acceleration (0-1023)
val.acceleration_old = val.acceleration;
}
}
// ISR
ISR(TIMER3_COMPA_vect)
{
// enginge control functions
ec(left);
ec(right);
// echo
Serial.println();
Serial.print("T");
Serial.print(millis()); // runtime
echo(&left);
echo(&right);
}
void setup(){
// turning on interrupts
sei();
// using timer 3 (16 bit)
// setting zero
TCCR3A = 0;
// setting zero
// setting prescaler to 1/1
// WGM to CTC
TCCR3B = 0;
TCCR3B |= _BV(CS30) | _BV(WGM32);
// set compare
OCR3A = 16000;
// on compare interrupt A running
TIMSK3 |= _BV(OCIE3A);
// set pins
pinMode(left.idle , OUTPUT);
pinMode(left.pull , OUTPUT);
pinMode(left.ena , OUTPUT);
pinMode(right.idle, OUTPUT);
pinMode(right.pull, OUTPUT);
pinMode(right.ena , OUTPUT);
// turn all pins off
digitalWrite(left.idle , LOW);
digitalWrite(left.pull , LOW);
digitalWrite(right.idle, LOW);
digitalWrite(right.pull, LOW);
// preset value for calculations
left.sensval_old = analogRead(left.horizontal);
right.sensval_old = analogRead(right.horizontal/);
// begin serial communication at 9600 bits of data per second
Serial.begin(9600);
}
void loop(){
}
so far the whole code
Now always when i am trying to do analogWrite(val.pull, val.outputval); the pin val.pull keeps beeing LOW tho val.outputval is not constantly 0. (changes by movement of potentiometer)
When i write analogWrite(val.pull, 255); or any other value it works perfectly... only when i want to write val.outputval it does not work
I really need to solve this problem till 02.05!
Any ideas?
greets
Sebastian