Hey, guys. ZZ Cat here. =^/.^=
So, I've got this idea where I'm usin' my UNO to read values off a pot, send that to my Micro down the I2C bus & the Micro converts that value to a signal pulse to an attached servo.
So! Problem: Twitching.
I've checked the serial monitor & it's only goin' from 0 to 44 & looping through that, despite the figure being mapped from 0-1023 to 0-179.
The first code document is the master transmitter that I've written up for my Arduino UNO:
/*
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/
////////////////////////////////////////\/
/\ Arduino I2C Servo Controller(Master) \/
/\ v1.0.0 \/
/\ \/
/\ Designed by: \/
/\ Cudby Æronautics International \/
/\////////////////////////////////////////
/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Description:
This is an I2C serial-based servo controller using the Arduino's UNO & Micro. The UNO is configured as a Master/Writer & the Micro configured as a Slave/Receiver.
The UNO converts analog values of an attached pot to a serial value & transmits that value to the Micro. The Micro then converts that received serial value to a PWM signal
to turn the servo according to the position of the potentiometer that's attached to the UNO.
The Circuit:
UNO:
> Is either powered by the USB lead or the VIN jack.
> Leg 1 of the pot is connected to A0 & is pulled high.
> Leg 2 of the pot is connected to A1 & analog values are read from this pin.
> Leg 3 of the pot is connected to A2 & is pulled low.
> A4 (UNO) is connected to D2 (Micro) with a 4k7 pull-up resistor on the SDA bus line.
> A5 (UNO) is connected to D3 (Micro) with a 4k7 pull-up resistor on the SCL bus line.
Micro:
> Is powered by the USB lead.
> D2 (Micro) is connected to A4 (UNO).
> D3 (Micro) is connected to A5 (UNO.
> D4 is connected to the SIG lead of the servo.
> The + lead of the servo is connected to the 5V pin.
> The - lead of the servo is connected to the GND pin.
*/
// Libraries:
#include <Wire.h> // Add the use of the I2C library. #hashtagsaregay
// Digital I/O Pins:
const int potHighPin = A0; // Leg 1 of the pot is connected to ADC0.
const int potLowPin = A2; // Leg 3 of the pot is connected to ADC2.
// ADC Pin:
const int potWiperPin = A1; // Leg 2 of the pot is connected to ADC1.
void setup() {
Serial.begin(9600); // Start the Serial Monitor at 9600BAUD.
Wire.begin(); // Start the I2C Bus. Address is not needed for the master device.
// Pot Properties:
pinMode(potHighPin, OUTPUT); // Configure Leg 1 of the pot as an output.
pinMode(potLowPin, OUTPUT); // Configure Leg 3 of the pot as an output.
digitalWrite(potHighPin, HIGH); // Drive Leg 1 of the pot high.
digitalWrite(potLowPin, LOW); // Drive Leg 3 of the pot low.
}
void loop() {
byte swep = analogRead(potWiperPin); // Read the wiper position as an analog value.
Wire.beginTransmission(4); // Transmit to device #4.
Wire.write(swep); // Send four bytes.
Wire.endTransmission(); // Stop Transmitting.
}
The second code document is the slave receiver that I've written up for my Arduino Micro:
/*
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/
////////////////////////////////////////\/
/\ Arduino I2C Servo Controller (Slave) \/
/\ v1.0.0 \/
/\ \/
/\ Designed by: \/
/\ Cudby Æronautics International \/
/\////////////////////////////////////////
/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Description:
This is an I2C serial-based servo controller using the Arduino's UNO & Micro. The UNO is configured as a Master/Writer & the Micro configured as a Slave/Receiver.
The UNO converts analog values of an attached pot to a serial value & transmits that value to the Micro. The Micro then converts that received serial value to a PWM signal
to turn the servo according to the position of the potentiometer that's attached to the UNO.
The Circuit:
UNO:
> Is either powered by the USB lead or the VIN jack.
> Leg 1 of the pot is connected to A0 & is pulled high.
> Leg 2 of the pot is connected to A1 & analog values are read from this pin.
> Leg 3 of the pot is connected to A2 & is pulled low.
> A4 (UNO) is connected to D2 (Micro) with a 4k7 pull-up resistor on the SDA bus line.
> A5 (UNO) is connected to D3 (Micro) with a 4k7 pull-up resistor on the SCL bus line.
Micro:
> Is powered by the USB lead.
> D2 (Micro) is connected to A4 (UNO).
> D3 (Micro) is connected to A5 (UNO.
> D4 is connected to the SIG lead of the servo.
> The + lead of the servo is connected to the 5V pin.
> The - lead of the servo is connected to the GND pin.
*/
// Libraries:
#include <Servo.h> // Add the use of the Servo library.
#include <Wire.h> // Add the use of the I2C library. #hashtagsaregay
// Servos:
Servo Not_a_servo; // Create a servo called "Not a servo."
void setup() {
Serial.begin(9600); // Start the Serial Monitor at 9600BAUD.
Wire.begin(4); // Start the I2C Bus as device #4.
Wire.onReceive(receiveEvent); // Register event.
Not_a_servo.attach(4); // Attach the SIG lead to D4.
}
void loop() {
}
void receiveEvent(int Range) {
if (Wire.available()) { // If there's data available at the I2C port...
int swep = Wire.read(); // Read the received data as an integer.
swep = map(swep, 0, 1023, 0, 180); // map the recieved data to the sweep range.
Not_a_servo.write(swep); // Turn the servo horn according to the recieved data.
Serial.println(swep); // Transmit the sweep angle to the serial monitor.
}
}
First screenie is the bottom end. IE when the pot is at it's lowest point:
Second screenie is the "top end" when I turn the pot about 1/8 of the way up:
Third & final screenie: Here's what happens if I turn the pot higher up...
So, that's what I suspect is causing the twitching. My question to you is... what can I do to fix this?